Dockerfile 实践 nginx镜像构建
先查看下本地的镜像,选取官网的centos作为base image:
[root@server ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 1e1148e4cc2c weeks ago 202MB
创建一个目录用于专门存放此demo的目录,也就是Dockerfile所在的目录
[root@server ~]# mkdir myNginx
[root@server ~]# cd myNginx/
[root@server myNginx]# touch Dockerfile
[root@server myNginx]# ll
总用量
-rw-r--r-- root root 1月 : Dockerfile
下面开始编写Dockerfile 文件,(注意Dockerfile的D需要大写)
v1版:
[root@server myNginx]# cat Dockerfile
# 指定基础镜像
FROM centos # MAINTAINER
MAINTAINER @qq.com # 安装基础工具包
RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data # 下载nginx
ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/ # 解压nginx 并创建用户
RUN tar -xvzf /opt/nginx/nginx-1.12..tar.gz -C /usr/local/src/ \
&& useradd -M -s /sbin/nologin nginx # 更改工作目录
WORKDIR /usr/local/src/nginx-1.12. # 编译安装nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install # 删除多余安装包
RUN rm -rf /opt/nginx/nginx-1.12..tar.gz # 设置环境变量
ENV PATH=/usr/local/nginx/sbin:$PATH # 设置端口
EXPOSE
执行docker build 进行构建:
[root@server myNginx]# ll
总用量
-rw-r--r-- root root 1月 : Dockerfile
[root@server myNginx]# docker build -t centos_nginx:v1 .
此处构建有点慢,因为需要安装编译nginx需要用到的软件,及下载nginx;后面的.代表的是相对路径的当前目录,如果需要全路径则为/root/myNginx(就是找到Dockerfile文件)
构建成功后,查看新构建的镜像:
[root@server myNginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v1 13d959cbec2b About a minute ago 504MB
centos latest 1e1148e4cc2c weeks ago 202MB
使用v1版本的镜像启动一个容器:
[root@server myNginx]# docker run -d -p : centos_nginx:v1 nginx -g "daemon off;"
961ef4274078b61a56667293ef69ee881fbf4171156c7990c94225eb508a172c
查看容器运行状态:
[root@server myNginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
961ef4274078 centos_nginx:v1 "nginx -g 'daemon of…" seconds ago Up seconds 0.0.0.0:->/tcp mystifying_goldberg
进行访问:
这样基于Dockerfile的一个简单的实例构建完成,现在基于这个Dockerfile文件依次添加其它指令进行构建
添加CMD 指令:
[root@server myNginx]# cat Dockerfile
# 指定基础镜像
FROM centos # MAINTAINER
MAINTAINER @qq.com # 安装基础工具包
RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data # 下载nginx
ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/ # 解压nginx 并创建用户
RUN tar -xvzf /opt/nginx/nginx-1.12..tar.gz -C /usr/local/src/ \
&& useradd -M -s /sbin/nologin nginx # 更改工作目录
WORKDIR /usr/local/src/nginx-1.12. # 编译安装nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install # 删除多余安装包
RUN rm -rf /opt/nginx/nginx-1.12..tar.gz # 设置环境变量
ENV PATH=/usr/local/nginx/sbin:$PATH # 设置端口
EXPOSE CMD /bin/sh -c 'nginx -g "daemon off;"'
然后进行构建v2版本:
[root@server myNginx]# docker build -t centos_nginx:v2 .
Sending build context to Docker daemon .072kB
Step / : FROM centos
---> 1e1148e4cc2c
Step / : MAINTAINER @qq.com
---> Using cache
---> d41475691003
Step / : RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data
---> Using cache
---> 7739a6cdd37c
Step / : ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/
Downloading [==================================================>] .7kB/.7kB
---> Using cache
---> a58676d797ad
Step / : RUN tar -xvzf /opt/nginx/nginx-1.12..tar.gz -C /usr/local/src/ && useradd -M -s /sbin/nologin nginx
---> Using cache
---> 7f671453fc20
Step / : WORKDIR /usr/local/src/nginx-1.12.
---> Using cache
---> be296a9725ea
Step / : RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
---> Using cache
---> fcbc1da221cc
Step / : RUN rm -rf /opt/nginx/nginx-1.12..tar.gz
---> Using cache
---> 2189ac28a36f
Step / : ENV PATH=/usr/local/nginx/sbin:$PATH
---> Using cache
---> 70faf0a90e9f
Step / : EXPOSE
---> Using cache
---> 13d959cbec2b
Step / : CMD /bin/sh -c 'nginx -g "daemon off;"'
---> Running in d07896b5ba56
Removing intermediate container d07896b5ba56
---> 3b71646263d3
Successfully built 3b71646263d3
Successfully tagged centos_nginx:v2
由于在构建的过程中docker 会采用缓存机制,上面构建过程中包含很多using cache,所以这次构件非常快,如果需要重新构建,不想使用cache 需要添加 --no-cache
查看v2版本的镜像:
[root@server myNginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v2 3b71646263d3 minutes ago 504MB
使用v2版本的镜像启动一个容器:
[root@server myNginx]# docker run -d -p : centos_nginx:v2
934ddc6f56a48f59b405c75c19b40ce5156388e34de37c078a56abc7c08a0602
然后查看容器状态:
[root@server myNginx]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
934ddc6f56a4 centos_nginx:v2 "/bin/sh -c '/bin/sh…" seconds ago Up seconds 0.0.0.0:->/tcp pedantic_cocks
进行访问:
新增加的CMD /bin/sh -c 'nginx -g "daemon off;"' 表示
当启动一个容器时候默认运行的命令,如果在启动容器时赋予了command的话,那么定义的CMD中的命令将不会被执行,而会去执行command的命令
添加ENTRYPOINT 指令:
[root@server myNginx]# cat Dockerfile
# 指定基础镜像
FROM centos # MAINTAINER
MAINTAINER @qq.com # 安装基础工具包
RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data # 下载nginx
ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/ # 解压nginx 并创建用户
RUN tar -xvzf /opt/nginx/nginx-1.12..tar.gz -C /usr/local/src/ \
&& useradd -M -s /sbin/nologin nginx # 更改工作目录
WORKDIR /usr/local/src/nginx-1.12. # 编译安装nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install # 删除多余安装包
RUN rm -rf /opt/nginx/nginx-1.12..tar.gz # 设置环境变量
ENV PATH=/usr/local/nginx/sbin:$PATH # 设置端口
EXPOSE ENTRYPOINT ["nginx"] CMD ["-g", "daemon off;"]
当ENTRYPOINT和CMD连用时,CMD的命令是ENTRYPOINT命令的参数,两者连用相当于nginx -g "daemon off;" ;而当一起连用的命令格式最好一致(此处选择的都是json格式)
然后进行构建v3版本:
[root@server myNginx]# docker build -t centos_nginx:v3 .
查看v3版本的镜像:
[root@server myNginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v3 1aeba273c0af seconds ago 504MB
使用v3版本的镜像启动一个容器:
[root@server myNginx]# docker run -d -p : centos_nginx:v3
2932d58ee8969587b04fa0155aadeb4b63a1934962d384624a8d7359725dfad3
然后查看容器状态:
[root@server myNginx]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2932d58ee896 centos_nginx:v3 "nginx -g 'daemon of…" minutes ago Up minutes 0.0.0.0:->/tcp boring_gauss
进行访问:
这里示例一个默认将nginx关闭的示例v3.1版:
[root@server myNginx]# cat Dockerfile
# 指定基础镜像
FROM centos # MAINTAINER
MAINTAINER @qq.com # 安装基础工具包
RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data # 下载nginx
ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/ # 解压nginx 并创建用户
RUN tar -xvzf /opt/nginx/nginx-1.12..tar.gz -C /usr/local/src/ \
&& useradd -M -s /sbin/nologin nginx # 更改工作目录
WORKDIR /usr/local/src/nginx-1.12. # 编译安装nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install # 删除多余安装包
RUN rm -rf /opt/nginx/nginx-1.12..tar.gz # 设置环境变量
ENV PATH=/usr/local/nginx/sbin:$PATH # 设置端口
EXPOSE ENTRYPOINT ["nginx"] CMD ["-g", "daemon on;"]
CMD 的命令修改为了后台,我们知道如果容器内的进程在后台运行那么容器将不会运行,构建v3.1版本:
[root@server myNginx]# docker build -t centos_nginx:v3. .
查看v3.1版本的镜像:
[root@server myNginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v3. ec17ab98a424 seconds ago 504MB
使用v3.1版本的镜像启动一个容器,但是在启动的时候添加后面的command:
[root@server myNginx]# docker run -d -p : centos_nginx:v3. -g "daemon off;"
0143d4b91ca1f97be31f0427140dfb17bb4ad9530d4a4a19b70c93044f6332c5
可以看见在后面新增了 -g "daemon off;",前面提过如果在启动容器时增加了命令,那么Dockerfile中的CMD中的命令将不会生效
查看容器运行状态:
[root@server myNginx]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0143d4b91ca1 centos_nginx:v3. "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:->/tcp loving_burnell
可以看见容器的运行丝毫没有问题,于是nginx 的服务依然还是在前台运行,没有被影响,而新增的command (-g "daemon off;")将作为ENTRYPOINT的新的参数以此为准,于是进行访问:
添加VOLUME指令:
[root@server myNginx]# cat Dockerfile
# 指定基础镜像
FROM centos # MAINTAINER
MAINTAINER @qq.com # 安装基础工具包
RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data # 下载nginx
ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/ # 解压nginx 并创建用户
RUN tar -xvzf /opt/nginx/nginx-1.12..tar.gz -C /usr/local/src/ \
&& useradd -M -s /sbin/nologin nginx # 挂载数据目录
VOLUME ["/usr/local/nginx/html"] # 更改工作目录
WORKDIR /usr/local/src/nginx-1.12. # 编译安装nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install # 删除多余安装包
RUN rm -rf /opt/nginx/nginx-1.12..tar.gz # 设置环境变量
ENV PATH=/usr/local/nginx/sbin:$PATH # 设置端口
EXPOSE ENTRYPOINT ["nginx"] CMD ["-g"]
然后进行构建v4版本:
[root@server myNginx]# docker build -t centos_nginx:v4 .
查看v4版本的镜像:
[root@server myNginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v4 b142047c1a6e seconds ago 504MB
使用v4版本的镜像启动一个容器:
[root@server myNginx]# docker run -d -p : --name nginx4 centos_nginx:v4 -g "daemon off;"
1d191f9bc49dc01ee2657508eba94ff1d02e5b4304eaf6ed5161e4b9000577ea
然后查看容器状态:
[root@server myNginx]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d191f9bc49d centos_nginx:v4 "nginx -g 'daemon of…" seconds ago Up seconds 0.0.0.0:->/tcp nginx4
通过docker inspect 可以看到宿主机上面的挂载目录路径:
[root@server ~]# docker inspect nginx4
"Mounts": [
{
"Type": "volume",
"Name": "5c4b8ec0bf7e4f243ea643af4af559617c89c43933768a1b4b45caef237251bf",
"Source": "/var/lib/docker/volumes/5c4b8ec0bf7e4f243ea643af4af559617c89c43933768a1b4b45caef237251bf/_data",
"Destination": "/usr/local/nginx/html",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
进入到宿主机的挂载目录并且查看目录内的文件:
[root@server ~]# cd /var/lib/docker/volumes/5c4b8ec0bf7e4f243ea643af4af559617c89c43933768a1b4b45caef237251bf/_data
[root@server _data]# pwd
/var/lib/docker/volumes/5c4b8ec0bf7e4f243ea643af4af559617c89c43933768a1b4b45caef237251bf/_data
[root@server _data]# ll
总用量
-rw-r--r-- 10月 50x.html
-rw-r--r-- 10月 index.html
利用docker exec 进入到容器中,查看卷:
[root@server myNginx]# docker exec -it nginx4 /bin/bash
[root@d2d2a2f287c6 nginx-1.12.]# cd /usr/local/nginx/html/
[root@d2d2a2f287c6 html]# pwd
/usr/local/nginx/html
[root@d2d2a2f287c6 html]# ll
total
-rw-r--r-- Oct 50x.html
-rw-r--r-- Oct index.html
现在在本地宿主机上的这个目录修改index.html文件内容:
[root@server _data]# echo "<h1>Hello Docker.</h1>" > index.html
[root@server _data]# cat index.html
<h1>Hello Docker.</h1>
然后切换到容器中查看这个文件是否发生改变:
[root@d2d2a2f287c6 html]# cat index.html
<h1>Hello Docker.</h1>
进行访问:
通过访问发现,在宿主机上面进行了更改,容器内部也发生了变化,这样就动态的实现网站数据动态更改。
添加ONBUILD 指令:
Dockerfile1中base image 为A镜像,并在Dockefile1中定义ONBUILD指令,构建成新的镜像B镜像
Dockerfile2中base image 为B镜像,构建成新镜像C
当使用镜像B启动容器1不会执行OBNUILD中定义的内容,而使用C镜像启动的容器2则会执行ONBUILD定义的内容
[root@server myNginx]# cat Dockerfile
# 指定基础镜像
FROM centos # MAINTAINER
MAINTAINER @qq.com # 安装基础工具包
RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data # 下载nginx
ADD http://nginx.org/download/nginx-1.12.2.tar.gz /opt/nginx/ # 解压nginx 并创建用户
RUN tar -xvzf /opt/nginx/nginx-1.12..tar.gz -C /usr/local/src/ \
&& useradd -M -s /sbin/nologin nginx # 挂载数据目录
ONBUILD VOLUME ["/data"] # 更改工作目录
WORKDIR /usr/local/src/nginx-1.12. # 编译安装nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install # 删除多余安装包
RUN rm -rf /opt/nginx/nginx-1.12..tar.gz # 设置环境变量
ENV PATH=/usr/local/nginx/sbin:$PATH # 设置端口
EXPOSE ENTRYPOINT ["nginx"] CMD ["-g"]
然后进行构建v5版本:
[root@server myNginx]# docker build -t centos_nginx:v5 .
使用v5版本的镜像启动一个容器:
[root@server myNginx]# docker run -d -p : --name nginx5 centos_nginx:v5 -g "daemon off;"
1f3c1672cce45e6f0f4e8d927de017fbe0a16f30858427d5cb3c2ec7f7b55d98
现在进入到nginx5容器内查看是否存在/data:
[root@server myNginx]# docker exec -it nginx5 /bin/bash
[root@1f3c1672cce4 nginx-1.12.]# ll /data
ls: cannot access /data: No such file or directory
现在修改上面的Dockerfile的FROM基于的base image:
[root@server myNginx]# cat Dockerfile
# 指定基础镜像
FROM centos_nginx:v5 # MAINTAINER
MAINTAINER @qq.com
然后进行构建v6版本:
[root@server myNginx]# docker build -t centos_nginx:v6 .
Sending build context to Docker daemon .096kB
Step / : FROM centos_nginx:v5
# Executing build trigger
---> Running in a34359a1107d
Removing intermediate container a34359a1107d
---> b1e2a7e80907
Step / : MAINTAINER @qq.com
---> Running in 901fa3500f51
Removing intermediate container 901fa3500f51
---> a32bd0c300e2
Successfully built a32bd0c300e2
Successfully tagged centos_nginx:v6
使用v6版本的镜像启动一个容器:
[root@server myNginx]# docker run -d -p : --name nginx6 centos_nginx:v6 -g "daemon off;"
f8ccf7ff63f875742ac93b54cc25a629e7df14c8e676e202def1191baf0b3c1f
查看容器状态,并进入容器验证/data/目录:
[root@server myNginx]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f8ccf7ff63f8 centos_nginx:v6 "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:->/tcp nginx6
[root@server myNginx]# docker exec -it nginx6 /bin/bash
[root@f8ccf7ff63f8 nginx-1.12.]# ll /data
total
由此可见镜像v6包含了v5所有的内容,并且增加了ONBUILD的内容