ONBUILD:当构建一个被继承的Dockerfile时运行的命令,父镜像在被子镜像继承后父镜像的onbuild被触发。可以把ONBUID理解为一个触发器。
[root@t-docker CMD]# cat Dockerfile2 from centos RUN yum -y install curl ENTRYPOINT ["curl","-s","http://ip.cn"] ONBUILD RUN echo "father images onbuild -----886" #表示哪个镜像继承了我,我就打出这句话
[root@t-docker CMD]# docker build -f Dockerfile2 -t myip_father . Sending build context to Docker daemon 3.072kB Step 1/4 : from centos ---> 5182e96772bf Step 2/4 : RUN yum -y install curl ---> Using cache ---> a35f6ad9700f Step 3/4 : ENTRYPOINT ["curl","-s","http://ip.cn"] ---> Using cache ---> b772af142e86 Step 4/4 : ONBUILD RUN echo "father images onbuild -----886" #表示哪个镜像继承了我,我就打出这句话。 ---> Running in 8366a71cb8ba Removing intermediate container 8366a71cb8ba ---> 8615f1d828ed Successfully built 8615f1d828ed Successfully tagged myip_father:latest
[root@t-docker CMD]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE myip_father latest 8615f1d828ed 28 seconds ago 293MB
[root@t-docker CMD]# cat Dockerfile3 from myip_father RUN yum -y install curl CMD ["curl","-s","http://ip.cn"]
[root@t-docker CMD]# docker build -f Dockerfile3 -t myip_son . Sending build context to Docker daemon 4.096kB Step 1/3 : from myip_father # Executing 1 build trigger ---> Running in c0ec856045b2 father images onbuild -----886 Removing intermediate container c0ec856045b2 ---> 533afe432759 Step 2/3 : RUN yum -y install curl ---> Running in 943e0afa4268 Loaded plugins: fastestmirror, ovl Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.huaweicloud.com * updates: mirrors.neusoft.edu.cn Package curl-7.29.0-46.el7.x86_64 already installed and latest version Nothing to do Removing intermediate container 943e0afa4268 ---> 54ddf5f440ec Step 3/3 : CMD ["curl","-s","http://ip.cn"] ---> Running in beff0a4f1b82 Removing intermediate container beff0a4f1b82 ---> 68d373aea823 Successfully built 68d373aea823 Successfully tagged myip_son:latest
上面看到在构建Dockerfile3时输出了# Executing 1 build trigger这句话。说明子镜像继承父镜像时,父镜像触发了相应的动作。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28916011/viewspace-2213399/ ,如需转载,请注明出处,否则将追究法律责任。