Dockerfile中ONBUILD的用法

来源:互联网 发布:草莓音乐节 上海 知乎 编辑:程序博客网 时间:2024/04/20 03:47

Dockerfile中ONBUILD的用法

root@ubuntu:~# cd /dockerfile/

root@ubuntu:/dockerfile#mkdir df_test7

root@ubuntu:/dockerfile# cd df_test7/

root@ubuntu:/dockerfile/df_test7#cp ../df_test6/* .

root@ubuntu:/dockerfile/df_test7# ll

-rw-r--r-- 1 root root  344 Jul 1 20:21 Dockerfile

-rw-r--r-- 1 root root  123 Jul 1 20:21 index.html

 

 

 

编辑Dockerfile

root@ubuntu:/dockerfile/df_test7# vim Dockerfile

root@ubuntu:/dockerfile/df_test7# cat Dockerfile

# 设置基本的镜像,后续命令都以这个镜像为基础 

FROM ubuntu

# 作者信息 

MAINTAINER shangwu 

# RUN命令会在上面指定的镜像里执行任何命令 

RUN apt-get update

RUN apt-get install -y nginx

 

#镜像触发器,当一个镜像被其他镜像作为基础镜像时执行

ONBUILD COPY index.html    /var/www/html/

#暴露ssh端口

EXPOSE 80 

ENTRYPOINT ["/usr/sbin/nginx","-g", "daemon off;"]

root@ubuntu:/dockerfile/df_test7#

 

 

 

构建容器df_test7

root@ubuntu:/dockerfile/df_test7#docker build -t="df_test7" .

Sending build context to Docker daemon 3.072 kB

Sending build context to Docker daemon

Step 0 : FROM ubuntu

 --->dc8dd8718e57

Step 1 : MAINTAINER shangwu

 --->Using cache

 --->cd3d00722426

Step 2 : RUN apt-get update

 --->Using cache

 --->0096fe9ac7c4

Step 3 : RUN apt-get install -y nginx

 --->Using cache

 --->c8b93cc747d7

Step 4 : ONBUILD copy index.html /var/www/html/

 --->Running in 88fae5c83cf0

 --->0b3453315eda

Removing intermediate container 88fae5c83cf0

Step 5 : EXPOSE 80

 --->Running in bef153e64cf4

 --->2337e81895b3

Removing intermediate container bef153e64cf4

Step 6 : ENTRYPOINT /usr/sbin/nginx -g daemon off;

 --->Running in d2e811ac26b7

 --->248999bd36af

Removing intermediate container d2e811ac26b7

Successfully built 248999bd36af

root@ubuntu:/dockerfile/df_test7#

说明:

         1)此镜像中命令ONBUILD copy index.html /var/www/html/ 不会生效;

         2)只有以此镜像df_test7为基础构建的镜像,上面的命令才可生效;

 

 

启动镜像df_test7容器ob_test1

root@ubuntu:/dockerfile/df_test7#docker run -p 80 -d --name ob_test1 df_test7

7d4f50689c9acb45f210a2da4087924d5c6ee948d47e73d7dd6b5f5c50fb64da

root@ubuntu:/dockerfile/df_test7#docker ps

CONTAINER ID       IMAGE               COMMAND                CREATED             STATUS              PORTS                   NAMES

7d4f50689c9a       df_test7:latest    "/usr/sbin/nginx -g    3seconds ago       Up 2 seconds        0.0.0.0:32771->80/tcp   ob_test1           

9e647530168f       df_test6:latest    "/usr/sbin/nginx -g    18minutes ago      Up 18 minutes       0.0.0.0:32770->80/tcp   cp_test 

 

访问ob_test1测试

root@ubuntu:/dockerfile/df_test7#curl http://127.0.0.1:32771

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

<style>

    body {

       width: 35em;

       margin: 0 auto;

       font-family: Tahoma, Verdana, Arial, sans-serif;

    }

</style>

</head>

<body>

<h1>Welcome to nginx!</h1>

<p>If you see this page, the nginx web serveris successfully installed and

working. Further configuration isrequired.</p>

 

<p>For online documentation and supportplease refer to

<ahref="http://nginx.org/">nginx.org</a>.<br/>

Commercial support is available at

<ahref="http://nginx.com/">nginx.com</a>.</p>

 

<p><em>Thank you for usingnginx.</em></p>

</body>

</html>

root@ubuntu:/dockerfile/df_test7#

 

 

 

 

 

构建Dockerfile镜像df_test8

root@ubuntu:/dockerfile/df_test7# cd

root@ubuntu:~# cd /dockerfile/

root@ubuntu:/dockerfile#mkdir df_test8

root@ubuntu:/dockerfile# cd df_test8/

root@ubuntu:/dockerfile/df_test8#vim Dockerfile

root@ubuntu:/dockerfile/df_test8#cat Dockerfile

# 设置基本的镜像,后续命令都以这个镜像为基础 

FROM df_test7

# 作者信息 

MAINTAINER shangwu 

#暴露ssh端口

EXPOSE 80 

ENTRYPOINT ["/usr/sbin/nginx","-g", "daemon off;"]

root@ubuntu:/dockerfile/df_test8#

root@ubuntu:/dockerfile/df_test8#cp ../df_test7/index.html .

root@ubuntu:/dockerfile/df_test8# ll

-rw-r--r--  1root root  204 Jul  1 20:24 Dockerfile

-rw-r--r--  1root root  123 Jul  1 20:25 index.html

root@ubuntu:/dockerfile/df_test8#

 

 

 

 

构建镜像容器df_test8

root@ubuntu:/dockerfile/df_test8#docker build -t="df_test8" .

Sending build context to Docker daemon 3.072 kB

Sending build context to Docker daemon

Step 0 : FROM df_test7

# Executing 1 build triggers

Trigger0, COPY index.html    /var/www/html/

Step 0 :COPY index.html /var/www/html/

 --->9d1762a43b42

Removing intermediate container f006323862e3

Step 1 : MAINTAINER shangwu

 --->Running in 7de810c3a98a

 --->91d10a5821d8

Removing intermediate container 7de810c3a98a

Step 2 : EXPOSE 80

 --->Running in eba65d3bd503

 --->5ecf5917a150

Removing intermediate container eba65d3bd503

Step 3 : ENTRYPOINT /usr/sbin/nginx -g daemon off;

 --->Running in f7a57fa65eb7

 --->8939ec499228

Removing intermediate container f7a57fa65eb7

Successfully built 8939ec499228

root@ubuntu:/dockerfile/df_test8#

 

 

 

 

启动容器ob_test2

root@ubuntu:/dockerfile/df_test8#docker run -p 80 -d --name ob_test2 df_test8

eb755486e3759d3db4ace182eddc4bd02026fe7b4a98db0dcf17641d06d0f45a

root@ubuntu:/dockerfile/df_test8# docker ps -l

CONTAINER ID       IMAGE               COMMAND                CREATED             STATUS              PORTS                   NAMES

eb755486e375       df_test8:latest    "/usr/sbin/nginx -g    4seconds ago       Up 3 seconds        0.0.0.0:32772->80/tcp   ob_test2

 

 

 

访问容器ob_test2中的nginx测试

root@ubuntu:/dockerfile/df_test8#curl http://127.0.0.1:32772

<html>

<head>

   <title>Page added in Dcokerfile</title>

</head>

<body>

   <h1>I'm page in df_test6</h1>

</body>

</html>

root@ubuntu:/dockerfile/df_test8#