docker之Dockerfile实践
<p>上一篇介绍了Dockerfile中使用的指令,现在开始进行指令实践</p><p>先查看下本地的镜像,选一个作为base image:</p>
<div class="cnblogs_code">
<pre># docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wadeson</span>/centos_nginx v1 210a202d37b8 <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)"> hours ago 464MB
nginx latest c59f17fe53b0 </span><span style="color: rgba(128, 0, 128, 1)">4</span><span style="color: rgba(0, 0, 0, 1)"> days ago 108MB
ubuntu latest 747cb2d60bbe </span><span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)"> weeks ago 122MB
centos latest 196e0ce0c9fb </span><span style="color: rgba(128, 0, 128, 1)">6</span> weeks ago 197MB</pre>
</div>
<p>在某一个目录下面创建一个专门存放此demo的目录,也就是Dockerfile所在的context:</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">mkdir</span><span style="color: rgba(0, 0, 0, 1)"> docker_demo
# cd docker_demo/<span style="color: rgba(0, 0, 0, 1)">
# </span><span style="color: rgba(0, 0, 255, 1)">touch</span><span style="color: rgba(0, 0, 0, 1)"> Dockerfile
# </span><span style="color: rgba(0, 0, 255, 1)">pwd</span>
/root/<span style="color: rgba(0, 0, 0, 1)">docker_demo
# ll
total </span><span style="color: rgba(128, 0, 128, 1)">0</span>
-rw-r--r--. <span style="color: rgba(128, 0, 128, 1)">1</span> root root <span style="color: rgba(128, 0, 128, 1)">0</span> Nov<span style="color: rgba(128, 0, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">04</span>:<span style="color: rgba(128, 0, 128, 1)">34</span> Dockerfile</pre>
</div>
<p>接下来就开始编写Dockerfile文件了(注意Dockerfile的D需要大写)</p>
<p>这里以编译nginx提供web服务来构建新的镜像</p>
<p>1、下载nginx源码包到docker_demo这个目录下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># ll
total </span><span style="color: rgba(128, 0, 128, 1)">960</span>
-rw-r--r--. <span style="color: rgba(128, 0, 128, 1)">1</span> root root <span style="color: rgba(128, 0, 128, 1)">0</span> Nov<span style="color: rgba(128, 0, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">04</span>:<span style="color: rgba(128, 0, 128, 1)">34</span><span style="color: rgba(0, 0, 0, 1)"> Dockerfile
</span>-rw-r--r--. <span style="color: rgba(128, 0, 128, 1)">1</span> root root <span style="color: rgba(128, 0, 128, 1)">981687</span> Oct <span style="color: rgba(128, 0, 128, 1)">17</span> <span style="color: rgba(128, 0, 128, 1)">09</span>:<span style="color: rgba(128, 0, 128, 1)">20</span> nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz</pre>
</div>
<p>2、以下是编写好的Dockerfile v1版:</p>
<div class="cnblogs_code">
<p># cat Dockerfile <br># base image<br>FROM centos</p>
<p># MAINTAINER<br>MAINTAINER json_hc@163.com</p>
<p># put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx<br>ADD nginx-1.12.2.tar.gz /usr/local/src</p>
<p># running required command<br>RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel <br>RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel<br>RUN useradd -M -s /sbin/nologin nginx</p>
<p># change dir to /usr/local/src/nginx-1.12.2<br>WORKDIR /usr/local/src/nginx-1.12.2</p>
<p># execute command to compile nginx<br>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</p>
<p>EXPOSE 80</p>
</div>
<p>3、查看docker_demo目录情况:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># ll
total </span><span style="color: rgba(128, 0, 128, 1)">964</span>
-rw-r--r--. <span style="color: rgba(128, 0, 128, 1)">1</span> root root <span style="color: rgba(128, 0, 128, 1)">1112</span> Nov<span style="color: rgba(128, 0, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">04</span>:<span style="color: rgba(128, 0, 128, 1)">58</span><span style="color: rgba(0, 0, 0, 1)"> Dockerfile
</span>-rw-r--r--. <span style="color: rgba(128, 0, 128, 1)">1</span> root root <span style="color: rgba(128, 0, 128, 1)">981687</span> Oct <span style="color: rgba(128, 0, 128, 1)">17</span> <span style="color: rgba(128, 0, 128, 1)">09</span>:<span style="color: rgba(128, 0, 128, 1)">20</span> nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz</pre>
</div>
<p>4、执行docker build进行构建:</p>
<div class="cnblogs_code">
<pre>docker build -t centos_nginx:v1 .</pre>
</div>
<p>后面的.代表的是相对路径的当前目录,如果需要全路径则为/root/docker_demo(就是找到Dockerfile文件)</p>
<p>构建成功后,查看新构建的镜像:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v1 78d18f16e757 </span><span style="color: rgba(128, 0, 128, 1)">4</span><span style="color: rgba(0, 0, 0, 1)"> hours ago 464MB
wadeson</span>/centos_nginx v1 210a202d37b8 <span style="color: rgba(128, 0, 128, 1)">7</span><span style="color: rgba(0, 0, 0, 1)"> hours ago 464MB
nginx latest c59f17fe53b0 </span><span style="color: rgba(128, 0, 128, 1)">4</span><span style="color: rgba(0, 0, 0, 1)"> days ago 108MB
ubuntu latest 747cb2d60bbe </span><span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)"> weeks ago 122MB
centos latest 196e0ce0c9fb </span><span style="color: rgba(128, 0, 128, 1)">6</span> weeks ago 197MB</pre>
</div>
<p>5、然后使用构建的镜像启动一个container并开启nginx服务:</p>
<div class="cnblogs_code">
<pre># docker run -d centos_nginx:v1 /usr/local/nginx/sbin/nginx -g <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">daemon off;</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
ea5af922378356a5ebff60992f000b186b09d1e8d6a4915b0b8ccf997ca12404</span></pre>
</div>
<p>然后查看启动的container是否在运行:</p>
<div class="cnblogs_code">
<pre># docker <span style="color: rgba(0, 0, 255, 1)">ps</span> -<span style="color: rgba(0, 0, 0, 1)">l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea5af9223783 centos_nginx:v1 </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/usr/local/nginx/...</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 128, 1)">7</span> seconds ago Up <span style="color: rgba(128, 0, 128, 1)">6</span> seconds <span style="color: rgba(128, 0, 128, 1)">80</span>/tcp flamboyant_carson</pre>
</div>
<p>虽然nginx服务开启了,但是port并没有进行映射到本机host,所以这个container并不能进行访问,重新启动一个进行了映射端口的容器</p>
<div class="cnblogs_code">
<pre># docker run -d -p80:<span style="color: rgba(128, 0, 128, 1)">80</span> centos_nginx:v1 /usr/local/nginx/sbin/nginx -g <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">daemon off;</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
e4b6e4846dedc6f130e028701c84828a635f3b367c0d500ebd947de16b1be0b2</span></pre>
</div>
<p>再次查看端口映射信息:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># docker port e4b6e4846ded
</span><span style="color: rgba(128, 0, 128, 1)">80</span>/tcp -> <span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span>:<span style="color: rgba(128, 0, 128, 1)">80</span></pre>
</div>
<p>于是进行访问:</p>
<p><img src="https://images2017.cnblogs.com/blog/1168897/201711/1168897-20171101214944701-2097058657.png" alt=""></p>
<p>于是基于Dockerfile的一个简单实例构建完成,现在基于这个Dockerfile文件依次添加其他的指令进行构建</p>
<p>添加ENV环境变量指令:</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">cat</span><span style="color: rgba(0, 0, 0, 1)"> Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER json_hc@</span><span style="color: rgba(128, 0, 128, 1)">163</span><span style="color: rgba(0, 0, 0, 1)">.com
# put nginx</span>-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz into /usr/local/<span style="color: rgba(0, 0, 0, 1)">src and unpack nginx
ADD nginx</span>-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz /usr/local/<span style="color: rgba(0, 0, 0, 1)">src
# running required command
RUN </span><span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -y <span style="color: rgba(0, 0, 255, 1)">gcc</span> <span style="color: rgba(0, 0, 255, 1)">gcc</span>-c++ glibc <span style="color: rgba(0, 0, 255, 1)">make</span> autoconf openssl openssl-<span style="color: rgba(0, 0, 0, 1)">devel
RUN </span><span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-<span style="color: rgba(0, 0, 0, 1)">devel
RUN useradd </span>-M -s /sbin/<span style="color: rgba(0, 0, 0, 1)">nologin nginx
# change </span><span style="color: rgba(0, 0, 255, 1)">dir</span> to /usr/local/src/nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
WORKDIR </span>/usr/local/src/nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
# execute command to compile nginx
RUN .</span>/configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-<span style="color: rgba(0, 0, 255, 1)">file</span>-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 && <span style="color: rgba(0, 0, 255, 1)">make</span> && <span style="color: rgba(0, 0, 255, 1)">make</span> <span style="color: rgba(0, 0, 255, 1)">install</span><span style="color: rgba(0, 0, 0, 1)">
ENV PATH </span>/usr/local/nginx/<span style="color: rgba(0, 0, 0, 1)">sbin:$PATH
EXPOSE </span><span style="color: rgba(128, 0, 128, 1)">80</span></pre>
</div>
<p>然后进行构建:</p>
<div class="cnblogs_code">
<pre># docker build -<span style="color: rgba(0, 0, 0, 1)">t centos_nginx:v2 .
Sending build context to Docker daemon</span><span style="color: rgba(128, 0, 128, 1)">985</span><span style="color: rgba(0, 0, 0, 1)">.6kB
Step </span><span style="color: rgba(128, 0, 128, 1)">1</span>/<span style="color: rgba(128, 0, 128, 1)">10</span><span style="color: rgba(0, 0, 0, 1)"> : FROM centos
</span>---><span style="color: rgba(0, 0, 0, 1)"> 196e0ce0c9fb
Step </span><span style="color: rgba(128, 0, 128, 1)">2</span>/<span style="color: rgba(128, 0, 128, 1)">10</span> : MAINTAINER json_hc@<span style="color: rgba(128, 0, 128, 1)">163</span><span style="color: rgba(0, 0, 0, 1)">.com
</span>---><span style="color: rgba(0, 0, 0, 1)"> Using cache
</span>---><span style="color: rgba(0, 0, 0, 1)"> cde1d7830106
Step </span><span style="color: rgba(128, 0, 128, 1)">3</span>/<span style="color: rgba(128, 0, 128, 1)">10</span> : ADD nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz /usr/local/<span style="color: rgba(0, 0, 0, 1)">src
</span>---><span style="color: rgba(0, 0, 0, 1)"> Using cache
</span>---><span style="color: rgba(0, 0, 0, 1)"> 1e4d16340af0
Step </span><span style="color: rgba(128, 0, 128, 1)">4</span>/<span style="color: rgba(128, 0, 128, 1)">10</span> : RUN <span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -y <span style="color: rgba(0, 0, 255, 1)">gcc</span> <span style="color: rgba(0, 0, 255, 1)">gcc</span>-c++ glibc <span style="color: rgba(0, 0, 255, 1)">make</span> autoconf openssl openssl-<span style="color: rgba(0, 0, 0, 1)">devel
</span>---><span style="color: rgba(0, 0, 0, 1)"> Using cache
</span>---><span style="color: rgba(0, 0, 0, 1)"> 405835ad9b0b
Step </span><span style="color: rgba(128, 0, 128, 1)">5</span>/<span style="color: rgba(128, 0, 128, 1)">10</span> : RUN <span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-<span style="color: rgba(0, 0, 0, 1)">devel
</span>---><span style="color: rgba(0, 0, 0, 1)"> Using cache
</span>---><span style="color: rgba(0, 0, 0, 1)"> 4002738cf7a6
Step </span><span style="color: rgba(128, 0, 128, 1)">6</span>/<span style="color: rgba(128, 0, 128, 1)">10</span> : RUN useradd -M -s /sbin/<span style="color: rgba(0, 0, 0, 1)">nologin nginx
</span>---><span style="color: rgba(0, 0, 0, 1)"> Using cache
</span>---><span style="color: rgba(0, 0, 0, 1)"> 02961c5c564d
Step </span><span style="color: rgba(128, 0, 128, 1)">7</span>/<span style="color: rgba(128, 0, 128, 1)">10</span> : WORKDIR /usr/local/src/nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>
---><span style="color: rgba(0, 0, 0, 1)"> Using cache
</span>---><span style="color: rgba(0, 0, 0, 1)"> f1da71a93c5e
Step </span><span style="color: rgba(128, 0, 128, 1)">8</span>/<span style="color: rgba(128, 0, 128, 1)">10</span> : RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-<span style="color: rgba(0, 0, 255, 1)">file</span>-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 && <span style="color: rgba(0, 0, 255, 1)">make</span> && <span style="color: rgba(0, 0, 255, 1)">make</span> <span style="color: rgba(0, 0, 255, 1)">install</span>
---><span style="color: rgba(0, 0, 0, 1)"> Using cache
</span>---><span style="color: rgba(0, 0, 0, 1)"> cd2ad4c45004
Step </span><span style="color: rgba(128, 0, 128, 1)">9</span>/<span style="color: rgba(128, 0, 128, 1)">10</span> : ENV PATH /usr/local/nginx/<span style="color: rgba(0, 0, 0, 1)">sbin:$PATH
</span>---> Running <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> 07ba2f7129bc
</span>---><span style="color: rgba(0, 0, 0, 1)"> 9588fa1058aa
Removing intermediate container 07ba2f7129bc
Step </span><span style="color: rgba(128, 0, 128, 1)">10</span>/<span style="color: rgba(128, 0, 128, 1)">10</span> : EXPOSE <span style="color: rgba(128, 0, 128, 1)">80</span>
---> Running <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> 473cd847154a
</span>---><span style="color: rgba(0, 0, 0, 1)"> 2031faf8894a
Removing intermediate container 473cd847154a
Successfully built 2031faf8894a
Successfully tagged centos_nginx:v2</span></pre>
</div>
<p>由于在构建的过程中docker会采用缓存的机制,上面的构建过程中包含很多using cache,所以这次构建非常快,如果需要重新构建,不想使用cache需要添加--no-cache</p>
<p>--no-cache Do not use cache when building the image</p>
<p>查看v2版本的镜像:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v2 2031faf8894a </span><span style="color: rgba(128, 0, 128, 1)">2</span> minutes ago 464MB</pre>
</div>
<p>使用v2版本的镜像启动一个container:</p>
<div class="cnblogs_code">
<pre># docker run -d -p81:<span style="color: rgba(128, 0, 128, 1)">80</span> centos_nginx:v2 nginx -g <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">daemon off;</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
da48b465b1b1a14824497d724eee52b8408270b3b5223c5dd7094b7c0cef211d</span></pre>
</div>
<p>查看container运行状态:</p>
<div class="cnblogs_code">
<pre># docker <span style="color: rgba(0, 0, 255, 1)">ps</span> -<span style="color: rgba(0, 0, 0, 1)">l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da48b465b1b1 centos_nginx:v2 </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx -g 'daemon ...</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 128, 1)">23</span> seconds ago Up <span style="color: rgba(128, 0, 128, 1)">22</span> seconds <span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span>:<span style="color: rgba(128, 0, 128, 1)">81</span>-><span style="color: rgba(128, 0, 128, 1)">80</span>/tcp determined_neumann</pre>
</div>
<p>进行访问:</p>
<p><img src="https://images2017.cnblogs.com/blog/1168897/201711/1168897-20171101215815091-474688798.png" alt=""></p>
<p>上述启动容器的过程中使用的命令docker run -d -p81:80 centos_nginx:v2 nginx -g "daemon off;"为什么这里使用的nginx而不是</p>
<p>/usr/local/nginx/sbin/nginx,因为在Dockerfile文化中执行了PATH=/usr/local/nginx/sbin:$PATH,添加到了环境变量</p>
<p> </p>
<p>添加指令CMD:</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">cat</span><span style="color: rgba(0, 0, 0, 1)"> Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER json_hc@</span><span style="color: rgba(128, 0, 128, 1)">163</span><span style="color: rgba(0, 0, 0, 1)">.com
# put nginx</span>-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz into /usr/local/<span style="color: rgba(0, 0, 0, 1)">src and unpack nginx
ADD nginx</span>-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz /usr/local/<span style="color: rgba(0, 0, 0, 1)">src
# running required command
RUN </span><span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -y <span style="color: rgba(0, 0, 255, 1)">gcc</span> <span style="color: rgba(0, 0, 255, 1)">gcc</span>-c++ glibc <span style="color: rgba(0, 0, 255, 1)">make</span> autoconf openssl openssl-<span style="color: rgba(0, 0, 0, 1)">devel
RUN </span><span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-<span style="color: rgba(0, 0, 0, 1)">devel
RUN useradd </span>-M -s /sbin/<span style="color: rgba(0, 0, 0, 1)">nologin nginx
# change </span><span style="color: rgba(0, 0, 255, 1)">dir</span> to /usr/local/src/nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
WORKDIR </span>/usr/local/src/nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
# execute command to compile nginx
RUN .</span>/configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-<span style="color: rgba(0, 0, 255, 1)">file</span>-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 && <span style="color: rgba(0, 0, 255, 1)">make</span> && <span style="color: rgba(0, 0, 255, 1)">make</span> <span style="color: rgba(0, 0, 255, 1)">install</span><span style="color: rgba(0, 0, 0, 1)">
ENV PATH </span>/usr/local/nginx/<span style="color: rgba(0, 0, 0, 1)">sbin:$PATH
EXPOSE </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">
CMD </span>/bin/<span style="color: rgba(0, 0, 255, 1)">sh</span> -c <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">nginx -g "daemon off;"</span><span style="color: rgba(128, 0, 0, 1)">'</span></pre>
</div>
<p>然后进行构建:</p>
<div class="cnblogs_code">
<pre># docker build -t centos_nginx:v3 .</pre>
</div>
<p>查看v3版本的镜像:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v3 0e49a2c0562f </span><span style="color: rgba(128, 0, 128, 1)">11</span> seconds ago 464MB</pre>
</div>
<p>然后基于v3版本的镜像启动一个container:</p>
<div class="cnblogs_code">
<pre># docker run -d -p82:<span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)"> centos_nginx:v3
d988c04d04f49b909f28e7b664be3959a0d51b951f1c1b04fcf5c716552b7c41</span></pre>
</div>
<p>查看启动的容器状态:</p>
<div class="cnblogs_code">
<pre># docker <span style="color: rgba(0, 0, 255, 1)">ps</span> -<span style="color: rgba(0, 0, 0, 1)">l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d988c04d04f4 centos_nginx:v3 </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/bin/sh -c '/bin/...</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 128, 1)">6</span> seconds ago Up <span style="color: rgba(128, 0, 128, 1)">5</span> seconds <span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span>:<span style="color: rgba(128, 0, 128, 1)">82</span>-><span style="color: rgba(128, 0, 128, 1)">80</span>/tcp optimistic_saha</pre>
</div>
<p>最后进行访问:</p>
<p><img src="https://images2017.cnblogs.com/blog/1168897/201711/1168897-20171101220437310-328967201.png" alt=""></p>
<p>新增加的CMD /bin/sh -c 'nginx -g "daemon off;"'表示</p>
<p>当启动一个container时默认运行的命令,如果在启动container时赋予了command的化,那么</p>
<p>定义的CMD中的命令将不会被执行,而会去执行command的命令</p>
<p> </p>
<p>添加entrypoint指令:</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">cat</span><span style="color: rgba(0, 0, 0, 1)"> Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER json_hc@</span><span style="color: rgba(128, 0, 128, 1)">163</span><span style="color: rgba(0, 0, 0, 1)">.com
# put nginx</span>-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz into /usr/local/<span style="color: rgba(0, 0, 0, 1)">src and unpack nginx
ADD nginx</span>-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz /usr/local/<span style="color: rgba(0, 0, 0, 1)">src
# running required command
RUN </span><span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -y <span style="color: rgba(0, 0, 255, 1)">gcc</span> <span style="color: rgba(0, 0, 255, 1)">gcc</span>-c++ glibc <span style="color: rgba(0, 0, 255, 1)">make</span> autoconf openssl openssl-<span style="color: rgba(0, 0, 0, 1)">devel
RUN </span><span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-<span style="color: rgba(0, 0, 0, 1)">devel
RUN useradd </span>-M -s /sbin/<span style="color: rgba(0, 0, 0, 1)">nologin nginx
# change </span><span style="color: rgba(0, 0, 255, 1)">dir</span> to /usr/local/src/nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
WORKDIR </span>/usr/local/src/nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
# execute command to compile nginx
RUN .</span>/configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-<span style="color: rgba(0, 0, 255, 1)">file</span>-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 && <span style="color: rgba(0, 0, 255, 1)">make</span> && <span style="color: rgba(0, 0, 255, 1)">make</span> <span style="color: rgba(0, 0, 255, 1)">install</span><span style="color: rgba(0, 0, 0, 1)">
ENV PATH </span>/usr/local/nginx/<span style="color: rgba(0, 0, 0, 1)">sbin:$PATH
EXPOSE </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">
ENTRYPOINT [</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">]
CMD [</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-g</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">daemon off;</span><span style="color: rgba(128, 0, 0, 1)">"</span>]</pre>
</div>
<p>当ENTRYPOINT和CMD连用时,CMD的命令是ENTRYPOINT命令的参数,两者连用相当于nginx -g "daemon off;"</p>
<p>而当一起连用的时候命令格式最好一致(这里选择的都是json格式的是成功的,如果都是sh模式可以试一下)</p>
<p>开始进行构建v4版本:</p>
<div class="cnblogs_code">
<pre># docker build -t centos_nginx:v4 .</pre>
</div>
<p>查看新建的镜像:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v4 6c5128aaff05 </span><span style="color: rgba(128, 0, 128, 1)">4</span> minutes ago 464MB</pre>
</div>
<p>为v4版本的镜像启动一个container:</p>
<div class="cnblogs_code">
<pre># docker run -d -p83:<span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)"> centos_nginx:v4
6933c78255f3cebe44d4d5d080caf8a2fde45ded2f9b333ec01cdfe98cd5f417</span></pre>
</div>
<p>然后查看容器状态:</p>
<div class="cnblogs_code">
<pre># docker <span style="color: rgba(0, 0, 255, 1)">ps</span> -<span style="color: rgba(0, 0, 0, 1)">l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6933c78255f3 centos_nginx:v4 </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx -g 'daemon ...</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 128, 1)">6</span> seconds ago Up <span style="color: rgba(128, 0, 128, 1)">5</span> seconds <span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span>:<span style="color: rgba(128, 0, 128, 1)">83</span>-><span style="color: rgba(128, 0, 128, 1)">80</span>/tcp zealous_euclid</pre>
</div>
<p>最后进行访问:</p>
<p><img src="https://images2017.cnblogs.com/blog/1168897/201711/1168897-20171101222146232-1534714177.png" alt=""></p>
<p>这里增加一个案例,如果Dockerfile修改成下面:</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">cat</span><span style="color: rgba(0, 0, 0, 1)"> Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER json_hc@</span><span style="color: rgba(128, 0, 128, 1)">163</span><span style="color: rgba(0, 0, 0, 1)">.com
# put nginx</span>-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz into /usr/local/<span style="color: rgba(0, 0, 0, 1)">src and unpack nginx
ADD nginx</span>-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz /usr/local/<span style="color: rgba(0, 0, 0, 1)">src
# running required command
RUN </span><span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -y <span style="color: rgba(0, 0, 255, 1)">gcc</span> <span style="color: rgba(0, 0, 255, 1)">gcc</span>-c++ glibc <span style="color: rgba(0, 0, 255, 1)">make</span> autoconf openssl openssl-<span style="color: rgba(0, 0, 0, 1)">devel
RUN </span><span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-<span style="color: rgba(0, 0, 0, 1)">devel
RUN useradd </span>-M -s /sbin/<span style="color: rgba(0, 0, 0, 1)">nologin nginx
# change </span><span style="color: rgba(0, 0, 255, 1)">dir</span> to /usr/local/src/nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
WORKDIR </span>/usr/local/src/nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
# execute command to compile nginx
RUN .</span>/configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-<span style="color: rgba(0, 0, 255, 1)">file</span>-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 && <span style="color: rgba(0, 0, 255, 1)">make</span> && <span style="color: rgba(0, 0, 255, 1)">make</span> <span style="color: rgba(0, 0, 255, 1)">install</span><span style="color: rgba(0, 0, 0, 1)">
ENV PATH </span>/usr/local/nginx/<span style="color: rgba(0, 0, 0, 1)">sbin:$PATH
EXPOSE </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">
ENTRYPOINT [</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">]
CMD [</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-g</span><span style="color: rgba(128, 0, 0, 1)">","daemon on;"</span>]</pre>
</div>
<p>CMD的命令修改为了后台,我们知道如果容器内的进程在后台运行那么容器将不会运行,现在以此构建v5版本镜像:</p>
<div class="cnblogs_code">
<pre># docker build -t centos_nginx:v5 .</pre>
</div>
<p>查看v5版本的新镜像:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v5 5c1131306686 </span><span style="color: rgba(128, 0, 128, 1)">29</span> seconds ago 464MB</pre>
</div>
<p>现在利用v5版本镜像启动一个container,但是在启动的时候添加后面的command:</p>
<div class="cnblogs_code">
<pre># docker run -d -p85:<span style="color: rgba(128, 0, 128, 1)">80</span> centos_nginx:v5 -g <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">daemon off;</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
5e11edbbd2a0e184f1766c435c0d9b01ef5d74b57e2e2c3a1efed0cf2a2e037b</span></pre>
</div>
<p>可以看见在后面新增了-g "daemon off;",前面说过如果增加了命令那么Dockerfile中的CMD中的命令将不会生效</p>
<p>查看容器运行状态:</p>
<div class="cnblogs_code">
<pre># docker <span style="color: rgba(0, 0, 255, 1)">ps</span> -<span style="color: rgba(0, 0, 0, 1)">l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e11edbbd2a0 centos_nginx:v5 </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx -g 'daemon ...</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 128, 1)">3</span> seconds ago Up <span style="color: rgba(128, 0, 128, 1)">3</span> seconds <span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span>:<span style="color: rgba(128, 0, 128, 1)">85</span>-><span style="color: rgba(128, 0, 128, 1)">80</span>/tcp nifty_bartik</pre>
</div>
<p>可以看见容器的运行丝毫没有问题,于是nginx的服务依然还是在前台运行,没有被CMD影响,而新增加的command(-g "daemon off;")</p>
<p>将作为ENTRYPOINT的新的参数以此为准,于是进行访问端口85</p>
<p><img src="https://images2017.cnblogs.com/blog/1168897/201711/1168897-20171101222929201-743437922.png" alt=""></p>
<p> 添加VOLUME指令:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># cat Dockerfile
# </span><span style="color: rgba(0, 0, 255, 1)">base</span><span style="color: rgba(0, 0, 0, 1)"> image
FROM centos
# MAINTAINER
MAINTAINER json_hc@</span><span style="color: rgba(128, 0, 128, 1)">163</span><span style="color: rgba(0, 0, 0, 1)">.com
# put nginx</span>-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.tar.gz into /usr/local/<span style="color: rgba(0, 0, 0, 1)">src and unpack nginx
ADD nginx</span>-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.tar.gz /usr/local/<span style="color: rgba(0, 0, 0, 1)">src
# running required command
RUN yum install </span>-y gcc gcc-c++ glibc make autoconf openssl openssl-<span style="color: rgba(0, 0, 0, 1)">devel
RUN yum install </span>-y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-<span style="color: rgba(0, 0, 0, 1)">devel
RUN useradd </span>-M -s /sbin/<span style="color: rgba(0, 0, 0, 1)">nologin nginx
# mount a dir to container
VOLUME [</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/data</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">]
# change dir to </span>/usr/local/src/nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
WORKDIR </span>/usr/local/src/nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
# execute command to compile nginx
RUN .</span>/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 &&<span style="color: rgba(0, 0, 0, 1)"> make install
# setup PATH
ENV PATH </span>/usr/local/nginx/<span style="color: rgba(0, 0, 0, 1)">sbin:$PATH
# EXPOSE
EXPOSE </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">
# the command of entrypoint
ENTRYPOINT [</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">]
CMD [</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-g</span><span style="color: rgba(128, 0, 0, 1)">"</span>]</pre>
</div>
<p>开始进行构建:</p>
<div class="cnblogs_code">
<pre># docker build -t centos_nginx:v6 .</pre>
</div>
<p>查看v6版本的镜像:</p>
<div class="cnblogs_code">
<pre># docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v6 959fdf4d4288 </span><span style="color: rgba(128, 0, 128, 1)">35</span> seconds ago 464MB</pre>
</div>
<p>利用该镜像启动一个container:</p>
<div class="cnblogs_code">
<pre># docker run -d -p <span style="color: rgba(128, 0, 128, 1)">86</span>:<span style="color: rgba(128, 0, 128, 1)">80</span> --name=nginx6 centos_nginx:v6 -g <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">daemon off;</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
6c15a9e93fb1421bdb7eddaabe439996e97415e85a003f80c1d8b4b2c5ee3ffa</span></pre>
</div>
<p>查看启动的容器状态:</p>
<div class="cnblogs_code">
<pre># docker <span style="color: rgba(0, 0, 255, 1)">ps</span> -<span style="color: rgba(0, 0, 0, 1)">l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c15a9e93fb1 centos_nginx:v6 </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx -g 'daemon ...</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 128, 1)">4</span> seconds ago Up <span style="color: rgba(128, 0, 128, 1)">4</span> seconds <span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span>:<span style="color: rgba(128, 0, 128, 1)">86</span>-><span style="color: rgba(128, 0, 128, 1)">80</span>/tcp nginx6</pre>
</div>
<p>利用docker exec进入到container中,查看是否存在卷/data:</p>
<div class="cnblogs_code">
<pre># ll
total </span><span style="color: rgba(128, 0, 128, 1)">16</span>
-rw-r--r--. <span style="color: rgba(128, 0, 128, 1)">8</span> root root <span style="color: rgba(128, 0, 128, 1)">15836</span> Sep <span style="color: rgba(128, 0, 128, 1)">11</span> <span style="color: rgba(128, 0, 128, 1)">15</span>:<span style="color: rgba(128, 0, 128, 1)">53</span> anaconda-<span style="color: rgba(0, 0, 0, 1)">post.log
lrwxrwxrwx. </span><span style="color: rgba(128, 0, 128, 1)">1</span> root root <span style="color: rgba(128, 0, 128, 1)">7</span> Sep <span style="color: rgba(128, 0, 128, 1)">11</span> <span style="color: rgba(128, 0, 128, 1)">15</span>:<span style="color: rgba(128, 0, 128, 1)">51</span> bin -> usr/<span style="color: rgba(0, 0, 0, 1)">bin
drwxr</span>-xr-x. <span style="color: rgba(128, 0, 128, 1)">2</span> root root <span style="color: rgba(128, 0, 128, 1)">6</span> Nov<span style="color: rgba(128, 0, 128, 1)">2</span> <span style="color: rgba(128, 0, 128, 1)">01</span>:<span style="color: rgba(128, 0, 128, 1)">42</span> data</pre>
</div>
<p>这个/data挂载的目录对应本机host的这个目录:</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">pwd</span>
/var/lib/docker/volumes/3490a9b7f9773b020343e82c1c4236d976b69eb6a80121eb80612d8c39e02820/_data</pre>
</div>
<p>现在在本机host上的这个目录创建一个文件:</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">touch</span> wadeson.<span style="color: rgba(0, 0, 255, 1)">sh</span><span style="color: rgba(0, 0, 0, 1)">
# ll
total </span><span style="color: rgba(128, 0, 128, 1)">0</span>
-rw-r--r--. <span style="color: rgba(128, 0, 128, 1)">1</span> root root <span style="color: rgba(128, 0, 128, 1)">0</span> Nov<span style="color: rgba(128, 0, 128, 1)">1</span> <span style="color: rgba(128, 0, 128, 1)">21</span>:<span style="color: rgba(128, 0, 128, 1)">45</span> wadeson.<span style="color: rgba(0, 0, 255, 1)">sh</span></pre>
</div>
<p>然后切换到container中查看是否存在这个文件:</p>
<div class="cnblogs_code">
<pre># ll /data/<span style="color: rgba(0, 0, 0, 1)">
total </span><span style="color: rgba(128, 0, 128, 1)">0</span>
-rw-r--r--. <span style="color: rgba(128, 0, 128, 1)">1</span> root root <span style="color: rgba(128, 0, 128, 1)">0</span> Nov<span style="color: rgba(128, 0, 128, 1)">2</span> <span style="color: rgba(128, 0, 128, 1)">01</span>:<span style="color: rgba(128, 0, 128, 1)">45</span> wadeson.<span style="color: rgba(0, 0, 255, 1)">sh</span></pre>
</div>
<p> </p>
<p>添加ONBUILD指令:</p>
<p> Dockerfile1中base image为A镜像,并在Dockerfile1中定义ONBUILD指令,构建成新镜像B镜像</p>
<p> Dockerfile2中base image为B镜像,构建成新镜像C</p>
<p> 当使用镜像B启动的container1不会执行OBNUILD中定义的内容,而使用C镜像启动的container2则会执行ONBUILD定义的内容</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">cat</span><span style="color: rgba(0, 0, 0, 1)"> Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER json_hc@</span><span style="color: rgba(128, 0, 128, 1)">163</span><span style="color: rgba(0, 0, 0, 1)">.com
# put nginx</span>-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz into /usr/local/<span style="color: rgba(0, 0, 0, 1)">src and unpack nginx
ADD nginx</span>-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz /usr/local/<span style="color: rgba(0, 0, 0, 1)">src
# running required command
RUN </span><span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -y <span style="color: rgba(0, 0, 255, 1)">gcc</span> <span style="color: rgba(0, 0, 255, 1)">gcc</span>-c++ glibc <span style="color: rgba(0, 0, 255, 1)">make</span> autoconf openssl openssl-<span style="color: rgba(0, 0, 0, 1)">devel
RUN </span><span style="color: rgba(0, 0, 255, 1)">yum</span> <span style="color: rgba(0, 0, 255, 1)">install</span> -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-<span style="color: rgba(0, 0, 0, 1)">devel
RUN useradd </span>-M -s /sbin/<span style="color: rgba(0, 0, 0, 1)">nologin nginx
# </span><span style="color: rgba(0, 0, 255, 1)">mount</span> a <span style="color: rgba(0, 0, 255, 1)">dir</span><span style="color: rgba(0, 0, 0, 1)"> to container
ONBUILD VOLUME [</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/data</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">]
# change </span><span style="color: rgba(0, 0, 255, 1)">dir</span> to /usr/local/src/nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
WORKDIR </span>/usr/local/src/nginx-<span style="color: rgba(128, 0, 128, 1)">1.12</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
# execute command to compile nginx
RUN .</span>/configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-<span style="color: rgba(0, 0, 255, 1)">file</span>-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 && <span style="color: rgba(0, 0, 255, 1)">make</span> && <span style="color: rgba(0, 0, 255, 1)">make</span> <span style="color: rgba(0, 0, 255, 1)">install</span><span style="color: rgba(0, 0, 0, 1)">
# setup PATH
ENV PATH </span>/usr/local/nginx/<span style="color: rgba(0, 0, 0, 1)">sbin:$PATH
# EXPOSE
EXPOSE </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">
# the command of entrypoint
ENTRYPOINT [</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">]
CMD [</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-g</span><span style="color: rgba(128, 0, 0, 1)">"</span>]</pre>
</div>
<p>使用上面Dockerfile构建镜像版本v7:</p>
<div class="cnblogs_code">
<pre># docker run -d -p <span style="color: rgba(128, 0, 128, 1)">87</span>:<span style="color: rgba(128, 0, 128, 1)">80</span> --name=nginx7 centos_nginx:v7 -g <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">daemon off;</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
48f1fe5c71aefc0f9513e8085af8f5b7cdf14fa986fb3b11f2050f18ceefd26e</span></pre>
</div>
<p>现在进入到容器内查看是否存在/data:</p>
<div class="cnblogs_code">
<pre># docker exec -it nginx7 /bin/<span style="color: rgba(0, 0, 0, 1)">bash
# ll /<span style="color: rgba(0, 0, 0, 1)">data
</span><span style="color: rgba(0, 0, 255, 1)">ls</span>: cannot access /data: No such <span style="color: rgba(0, 0, 255, 1)">file</span> or directory</pre>
</div>
<p>现在修改上面Dockerfile的FROM基于的base image:</p>
<div class="cnblogs_code">
<p># cat Dockerfile<br># base image<br>FROM centos_nginx:v7</p>
<p># MAINTAINER<br>MAINTAINER json_hc@163.com</p>
</div>
<p>利用此Dockerfile构建镜像v8:</p>
<div class="cnblogs_code">
<pre># docker build -<span style="color: rgba(0, 0, 0, 1)">t centos_nginx:v8 .
Sending build context to Docker daemon</span><span style="color: rgba(128, 0, 128, 1)">986</span><span style="color: rgba(0, 0, 0, 1)">.6kB
Step </span><span style="color: rgba(128, 0, 128, 1)">1</span>/<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)"> : FROM centos_nginx:v7
# Executing </span><span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)"> build trigger...
Step </span><span style="color: rgba(128, 0, 128, 1)">1</span>/<span style="color: rgba(128, 0, 128, 1)">1</span> : VOLUME /<span style="color: rgba(0, 0, 0, 1)">data
</span>---> Running <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> a6187867513d
</span>---><span style="color: rgba(0, 0, 0, 1)"> 5ac07930be4c
Removing intermediate container a6187867513d
Step </span><span style="color: rgba(128, 0, 128, 1)">2</span>/<span style="color: rgba(128, 0, 128, 1)">2</span> : MAINTAINER json_hc@<span style="color: rgba(128, 0, 128, 1)">163</span><span style="color: rgba(0, 0, 0, 1)">.com
</span>---> Running <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> e02dbf8219cf
</span>---><span style="color: rgba(0, 0, 0, 1)"> 6f792dc07c35
Removing intermediate container e02dbf8219cf
Successfully built 6f792dc07c35
Successfully tagged centos_nginx:v8</span></pre>
</div>
<p>可以看见卷/data已经执行了操作,现在启动一个container:</p>
<div class="cnblogs_code">
<pre># docker run -d -p <span style="color: rgba(128, 0, 128, 1)">88</span>:<span style="color: rgba(128, 0, 128, 1)">80</span> --name=nginx8 centos_nginx:v8 -g <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">daemon off;</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
6c2a847c5f6b59b02f91afecadbfc15c88d1217a477c0421a424bce6e5eb317a</span></pre>
</div>
<p>查看容器状态,并进入到容器验证/data目录:</p>
<div class="cnblogs_code">
<pre># docker <span style="color: rgba(0, 0, 255, 1)">ps</span> -<span style="color: rgba(0, 0, 0, 1)">l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c2a847c5f6b centos_nginx:v8 </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nginx -g 'daemon ...</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 128, 1)">37</span> seconds ago Up <span style="color: rgba(128, 0, 128, 1)">37</span> seconds <span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span>:<span style="color: rgba(128, 0, 128, 1)">88</span>-><span style="color: rgba(128, 0, 128, 1)">80</span>/<span style="color: rgba(0, 0, 0, 1)">tcp nginx8
# docker exec </span>-it nginx8 /bin/<span style="color: rgba(0, 0, 0, 1)">bash
# ll /data/<span style="color: rgba(0, 0, 0, 1)">
total </span><span style="color: rgba(128, 0, 128, 1)">0</span></pre>
</div>
<p>由此可见镜像v8包含了v7 的所有内容,并且增加了ONBUILD的内容</p><br><br>
来源:https://www.cnblogs.com/jsonhc/p/7767669.html
頁:
[1]