Nginx是一个高性能的Web和反向代理服务器,它具有很多非常优越的特性:
1、作为Web服务器。
2、作为负载均衡服务器。
3、作为邮件代理服务器。
4、安装及配置简单。
接下来我们介绍在docker构建nginx镜像:
Docker镜像构建分为两种方式:

  1. 手动构建
  2. Dockerfile(自动构建)

一、Docker镜像手动构建实例

  1. 基于centos镜像进行构建nginx镜像
  2. #下载镜像
  3. [root@compute ~]# docker pull centos
  4. [root@compute ~]# docker pull nginx
  5. [root@compute ~]# docker images
  6. REPOSITORY TAG IMAGE ID CREATED SIZE
  7. docker.io/centos latest ff426288ea90 13 days ago 207.2 MB
  8. docker.io/nginx latest 3f8a4339aadd 3 weeks ago 108.5 MB
  9. #先创建centos镜像
  10. [root@compute ~]# docker run --name nginxdocker -ti centos
  11. [root@10859f0ffd78 /]# yum install -y wget
  12. [root@10859f0ffd78 /]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  13. #安装nginx
  14. [root@10859f0ffd78 /]# yum install -y nginx
  15. [root@10859f0ffd78 /]# sed -i '3a daemon off;' /etc/nginx/nginx.conf
  16. [root@10859f0ffd78 /]# exit
  17. [root@compute ~]# docker ps -a
  18. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  19. 10859f0ffd78 centos "/bin/bash" 7 minutes ago Exited (0) 19 seconds ago nginxdocker
  20. #构建镜像
  21. [root@compute ~]# docker commit -m "test Nginx" 10859f0ffd78 nginxdocker/nginxdocker:v1
  22. sha256:616e9d624b9a1db8e23491db331228ca56dd60c04356da14ab6d7e4cf821d415
  23. You have new mail in /var/spool/mail/root
  24. [root@compute ~]# docker images
  25. REPOSITORY TAG IMAGE ID CREATED SIZE
  26. nginxdocker/nginxdocker v1 616e9d624b9a 10 seconds ago 383.9 MB
  27. #启动容器
  28. [root@compute ~]# docker run --name nginxserver -d -p 82:80 nginxdocker/nginxdocker:v1 nginx
  29. c2ded9ce8af76c3b4862ca54478d39429879c856a2751957c9287df077dfcf17
  30. [root@compute ~]# docker ps
  31. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  32. c2ded9ce8af7 nginxdocker/nginxdocker:v1 "nginx" 21 seconds ago Up 19 seconds 0.0.0.0:82->80/tcp nginxserver
  33. #测试
  34. [root@compute ~]# curl -I 192.168.128.165:82
  35. HTTP/1.1 200 OK
  36. Server: nginx/1.12.2
  37. Date: Mon, 22 Jan 2018 08:15:39 GMT
  38. Content-Type: text/html
  39. Content-Length: 3700
  40. Last-Modified: Wed, 18 Oct 2017 08:08:18 GMT
  41. Connection: keep-alive
  42. ETag: "59e70bf2-e74"
  43. Accept-Ranges: bytes

二、Docker镜像自动构建实例

Dockerfile是一个文本格式的配置文件,用户可以使用Dockerfile快速创建自定义的镜像。
Dockerfile由一行行命令语句组成,#号注释。分为:基础镜像信息、维护者信息、镜像操作指令和容器启动时执行指令。

  1. #创建目录
  2. [root@compute ~]# mkdir /dokerfile
  3. [root@compute ~]# cd /dokerfile/
  4. [root@compute dokerfile]# mkdir nginx
  5. [root@compute dokerfile]# cd nginx/
  6. #添加Dockerfile文件注意D大写
  7. [root@compute nginx]# vim Dockerfile
  8. #This dockerfile uses the centos image
  9. #VERSION 2 - EDITION 1
  10. #Author:jianlaihe
  11. #Command format:
  12. # 指定基于的基础镜像
  13. FROM centos
  14.  
  15. #维护者信息
  16. MAINTAINER jianlaihe hejianlai@dnion.com
  17.  
  18. #镜像的操作指令
  19. RUN yum install -y wget
  20. RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/re
  21. po/epel-7.repo
  22. RUN yum install -y nginx
  23. RUN echo "daemon off;" >>/etc/nginx/nginx.conf
  24. #添加文件需存在当前目录下
  25. ADD index.html /usr/share/nginx/html/index.html
  26. EXPOSE 80
  27. #容器启动时执行命令
  28. CMD /usr/sbin/nginx
  29.  
  30. [root@compute nginx]# echo "hello docker" >index.html
  31. #docker build命令构建镜像名为nginx版本v2
  32. [root@compute nginx]# docker build -t nginx:v2 .
  33. Complete!
  34. ---> 5e37422db8b2
  35. Removing intermediate container 87fa82c1173a
  36. Step 6 : RUN echo "daemon off;" >>/etc/nginx/nginx.conf
  37. ---> Running in a03da9a96436
  38. ---> 236590c11b39
  39. Removing intermediate container a03da9a96436
  40. Step 7 : ADD index.html /usr/share/nginx/html/index.html
  41. ---> ac28fc354cad
  42. Removing intermediate container 5c2d9944e6f3
  43. Step 8 : EXPOSE 80
  44. ---> Running in 0b598a0680b8
  45. ---> d4addb2c20ba
  46. Removing intermediate container 0b598a0680b8
  47. Step 9 : CMD /usr/sbin/nginx
  48. ---> Running in d1feaede849f
  49. ---> 4905d9869aa7
  50. Removing intermediate container d1feaede849f
  51. Successfully built 4905d9869aa7
  52. [root@compute nginx]# docker images
  53. REPOSITORY TAG IMAGE ID CREATED SIZE
  54. nginx v2 4905d9869aa7 53 seconds ago 404.1 MB
  55. #启动容器
  56. [root@compute nginx]# docker run --name testnginx -d -p 83:80 nginx:v2
  57. b2cd72936465464bb8a88e9b3c5df0015241c7d17cb74062433ef79582c58908
  58. #测试
  59. [root@compute nginx]# curl 192.168.128.165:83
  60. hello docker

Docker学习之4——构建NGINX镜像的更多相关文章

  1. 用Docker构建Nginx镜像

    1构建Nginx镜像 1建立工作目录 [root@localhost ]# mkdir 1nginx [root@localhost 1nginx]# cd 1nginx/ [root@localho ...

  2. 使用dockerfile构建nginx镜像

    使用dockerfile构建nginx镜像 docker构建镜像的方法:   commit.dockerfile 1.使用commit来构建镜像: commit是基于原有镜像基础上构建的镜像,使用此方 ...

  3. 使用dockerfile构建nginx镜像 转

      docker构建镜像的方法:   commit.dockerfile 1.使用commit来构建镜像: commit是基于原有镜像基础上构建的镜像,使用此方法构建镜像的目的:保存镜像里的一些配置信 ...

  4. Dockerfile构建nginx镜像

    Dockerfile构建nginx镜像 [root@bogon ~]# mkdir /opt/docker-file [root@bogon ~]# cd /opt/docker-file/ [roo ...

  5. docker学习笔记(3)- 镜像

    简介 在docker学习笔记(1)- 架构概述一节中可以看到镜像是docker三大组件之一,可以将Docker镜像类比为虚拟机的模版. 镜像由多个层组成,每层叠加之后从外部看就像一个独立的对象,镜像的 ...

  6. Docker学习总结(二)—— 镜像,容器

    1.Docker镜像  1.1相关概念:registry :用于保存Docker镜像,包括镜像层次结构和镜像元数据,类似于git仓库之类的实体. repository:某个Docker镜像所有迭代版本 ...

  7. docker学习7-Dockerfile制作自己的镜像文件

    前言 如果你是一个python自动化测试人员,某天你在公司终于完成了一个项目的接口自动化脚本工作,在你自己常用的本机或者服务器上调试完成了脚本,稳稳地没问题. 可是晚上下班回家,你自己找了个linux ...

  8. Docker 学习之部署php + nginx(一)

    博主电脑系统是window 10 专业版的,所以在此记录下docker的基本使用方法. 参考地址: https://www.runoob.com/docker/docker-install-php.h ...

  9. Docker学习笔记 - 创建私有的镜像仓库

    一.查找镜像仓库 https://hub.docker.com/ 二.下载镜像仓库 docker pull registry:2.6.2 三.安装镜像仓库 docker run -d -p 6000: ...

随机推荐

  1. 导出word文档 通过DocX组件

    根据DocX官方描述如下: In the application development process, it uses COM libraries and requires MS Word or ...

  2. POJ-2387.Til the Cows Come Home.(五种方法:Dijkstra + Dijkstra堆优化 + Bellman-Ford + SPFA + Floyd-Warshall)

    昨天刚学习完最短路的算法,今天开始练题发现我是真的菜呀,居然能忘记邻接表是怎么写的,真的是菜的真实...... 为了弥补自己的菜,我决定这道题我就要用五种办法写出,并在Dijkstra算法堆优化中另外 ...

  3. Curator的cluster,实现多节点数据共享

    模拟两个客户端,实现多节点数据共享 package bjsxt.curator.cluster; import org.apache.curator.RetryPolicy; import org.a ...

  4. [leetcode]380. Insert Delete GetRandom O(1)常数时间插入删除取随机值

    Design a data structure that supports all following operations in average O(1) time. insert(val): In ...

  5. tiny4412 --uboot移植(2) 点灯

    开发环境:win10 64位 + VMware12 + Ubuntu14.04 32位 工具链:linaro提供的gcc-linaro-6.1.1-2016.08-x86_64_arm-linux-g ...

  6. Loadrunner登录判断

    loadrunner判断登录是否成功,以下方法可以解决:1.利用添加检查点web_reg_find函数的方法---------------------------- lr_start_transact ...

  7. leveldb 学习记录(四)Log文件

    前文记录 leveldb 学习记录(一) skiplistleveldb 学习记录(二) Sliceleveldb 学习记录(三) MemTable 与 Immutable Memtablelevel ...

  8. win7启动时怎么自动进入桌面

    1.按Win+R组合键,打开“运行”对话框.(Win是键盘下方左右两边的两个印有微软标志的键) 2.Windows XP/2003/2008/2008R2输入"control userpas ...

  9. javascript常见内存泄露

    一.全局变量引起的内存泄漏 function func(){ lmw = 123456 //lmw是全局变量,不会被释放 } 二.闭包引起的内存泄漏 function func(){ var lmw ...

  10. GIS中空间数据和非空间数据