ONBUILD是 当目前的镜像在基于父镜像的基础上创建的时候,执行了父镜像的onbuild语句,
FROM 父镜像 而不是 创建父镜像的dockerfile
//编写父镜像
[root@test mydocker]# cat fatherfile
FROM centos
RUN yum install -y curl
ENTRYPOINT ["curl","-s","http://ip.cn"]
ONBUILD RUN echo "father is good"
//创建father名称的镜像
[root@test mydocker]# docker build -f /mydocker/fatherfile -t father .
Sending build context to Docker daemon 6.144kB
Step 1/4 : FROM centos
---> 300e315adb2f
Step 2/4 : RUN yum install -y curl
---> Using cache
---> 5f37af576672
Step 3/4 : ENTRYPOINT ["curl","-s","http://ip.cn"]
---> Using cache
---> adf1e62f8da6
Step 4/4 : ONBUILD RUN echo "father is good"
---> Using cache
---> fbb09770b5ed
Successfully built fbb09770b5ed
Successfully tagged father:latest
//编写子dockerfile文件继承父镜像
[root@test mydocker]# cat sonfile
//子镜像的dockerfile继承的是创建出来的父镜像,而不是创建父镜像用的dockerfile文件
FROM father
RUN yum install -y curl
ENTRYPOINT ["curl","-s","http://ip.cn"]
ONBUILD RUN echo "father is good"
//创建子镜像
[root@test mydocker]# docker build -f /mydocker/sonfile -t son .
Sending build context to Docker daemon 7.168kB
//继承父镜像
Step 1/4 : FROM father
# Executing 1 build trigger
---> Running in 7ca9d7f848c9
//执行了创建父镜像的onbuild语句
father is good
Removing intermediate container 7ca9d7f848c9
---> a480887d7a79
Step 2/4 : RUN yum install -y curl
---> Running in b57ba10115fc
Last metadata expiration check: 8:12:35 ago on Tue Jan 26 14:51:14 2021.
Package curl-7.61.1-14.el8_3.1.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
Removing intermediate container b57ba10115fc
---> eaa5d942d3a2
Step 3/4 : ENTRYPOINT ["curl","-s","http://ip.cn"]
---> Running in c5c07c614ee2
Removing intermediate container c5c07c614ee2
---> 89ef8472381d
Step 4/4 : ONBUILD RUN echo "father is good"
---> Running in 02fa4990dd3f
Removing intermediate container 02fa4990dd3f
---> f0d5a065fb1e
Successfully built f0d5a065fb1e
Successfully tagged son:latest
[root@test mydocker]#