dockerfile中ONBUILD指令的功能简介说明
下文笔者讲述dockerfile中ONBUILD命令的功能简介说明,如下所示
创建Dockerfile文件
Dockerfile_test 内容如下
ONBUILD指令简介
ONBUILD是一个特殊的指令 它后面跟的是其它指令 如 RUN, COPY 等 这些指令在当前镜像构建时并不会被执行 以当前镜像为基础镜像 去构建下一级镜像的时候 才会执行ONBUILD后面的指令
ONBUILD指令的使用
例:创建Dockerfile文件
Dockerfile_test 内容如下
FROM centos # 初始镜像 RUN yum install -y curl # 执行安装curl命令 ENTRYPOINT ["curl", "-s", "http://ip.cn"] # 容器启动后执行的命令 ONBUILD RUN echo "father---image---onbuild====886" 如: 假设有哪一个镜像继承了该镜像(父镜像) 当子镜像build的时候,就会触发父镜像的ONBUILD指令 使用Dockerfile_onbuild文件生成onbuild/father镜像,如下: [root@192 mydockerfile]# docker build -f /home/mydockerfile/Dockerfile_test -t onbuild/father:1.0 . Sending build context to Docker daemon 5.12kB Step 1/4 : FROM centos ---> 8129tde21s0d Step 2/4 : RUN yum install -y curl ---> Running in 11cd05afe92c CentOS Linux 8 - AppStream 2.5 MB/s | 6.3 MB 00:02 CentOS Linux 8 - BaseOS 1.0 MB/s | 2.3 MB 00:02 CentOS Linux 8 - Extras 2.3 kB/s | 9.2 kB 00:03 Package curl-7.61.1-14.el8.x86_64 is already installed. Dependencies resolved. ================================================================================ Package Architecture Version Repository Size ================================================================================ Upgrading: curl x86_64 7.61.1-14.el8_3.1 baseos 353 k libcurl-minimal x86_64 7.61.1-14.el8_3.1 baseos 285 k Transaction Summary ================================================================================ Upgrade 2 Packages Total download size: 638 k Downloading Packages: (1/2): curl-7.61.1-14.el8_3.1.x86_64.rpm 546 kB/s | 353 kB 00:00 (2/2): libcurl-minimal-7.61.1-14.el8_3.1.x86_64 406 kB/s | 285 kB 00:00 -------------------------------------------------------------------------------- Total 547 kB/s | 638 kB 00:01 CentOS Linux 8 - BaseOS 1.6 MB/s | 1.6 kB 00:00 warning: /var/cache/dnf/baseos-f6a80ba95cf937f2/packages/curl-7.61.1-14.el8_3.1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY Importing GPG key 0x8483C65D: Userid : "CentOS (CentOS Official Signing Key)" Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Upgrading : libcurl-minimal-7.61.1-14.el8_3.1.x86_64 1/4 Upgrading : curl-7.61.1-14.el8_3.1.x86_64 2/4 Cleanup : curl-7.61.1-14.el8.x86_64 3/4 Cleanup : libcurl-minimal-7.61.1-14.el8.x86_64 4/4 Running scriptlet: libcurl-minimal-7.61.1-14.el8.x86_64 4/4 Verifying : curl-7.61.1-14.el8_3.1.x86_64 1/4 Verifying : curl-7.61.1-14.el8.x86_64 2/4 Verifying : libcurl-minimal-7.61.1-14.el8_3.1.x86_64 3/4 Verifying : libcurl-minimal-7.61.1-14.el8.x86_64 4/4 Upgraded: curl-7.61.1-14.el8_3.1.x86_64 libcurl-minimal-7.61.1-14.el8_3.1.x86_64 Complete! Removing intermediate container 11cd05afe92c ---> c88555f994f9 Step 3/4 : ENTRYPOINT ["curl", "-s", "http://ip.cn"] ---> Running in 9d9061a8d47e Removing intermediate container 9d9061a8d47e ---> 96a5f70b56bf Step 4/4 : ONBUILD RUN echo "father---image---onbuild====886" ---> Running in 68ca17cc9a0b Removing intermediate container 68ca17cc9a0b ---> 05fdd64f3e60 Successfully built 05fdd64f3e60 Successfully tagged onbuild/father:1.0 # 查看本地Docker镜像 [root@192 mydockerfile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE onbuild/father 1.0 05fdd64f3e60 42 seconds ago 243MB centos latest 300e315adb2f 3 months ago 209MB (2)开始演示 我们再创建一个Dockerfile文件:Dockerfile_onbuild_son。 [root@192 mydockerfile]# pwd /home/mydockerfile [root@192 mydockerfile]# touch Dockerfile_onbuild_son 编辑Dockerfile_onbuild_son文件内容如下: # 这里要继承上面生成的onbuild/father镜像 # 这样onbuild/father镜像就成了父镜像 # 通过该Dockerfile文件生成的镜像就是子镜像。 FROM onbuild/father:1.0 # 注意这里一定要加TAG版本,不然无法找到该镜像。 RUN yum install -y curl # 执行安装curl命令 CMD /bin/bash 使用Dockerfile_onbuild_son文件生成onbuild/son镜像,如下: [root@192 mydockerfile]# docker build -f /home/mydockerfile/Dockerfile_onbuild_son -t onbuild/son:1.0 . Sending build context to Docker daemon 6.144kB Step 1/3 : FROM onbuild/father:1.0 # Executing 1 build trigger # 看这三行,就是ONBUILD指令的效果 ---> Running in 0a165ca6352c # Executing 1 build trigger 执行1个构建触发器 father---image---onbuild====886 # 数据了父镜像ONBUILD指令执行的内容 Removing intermediate container 0a165ca6352c ---> f9313959d1ae Step 2/3 : RUN yum install -y curl ---> Running in 0485b0d9d031 Last metadata expiration check: 0:09:32 ago on Sat Mar 20 14:46:45 2021. Package curl-7.61.1-14.el8_3.1.x86_64 is already installed. Dependencies resolved. Nothing to do. Complete! Removing intermediate container 0485b0d9d031 ---> 0718c9a2d99a Step 3/3 : CMD /bin/bash ---> Running in e102da5c1b5e Removing intermediate container e102da5c1b5e ---> 46ed53584363 Successfully built 46ed53584363 Successfully tagged onbuild/son:1.0 以上就是ONBUILD指令的使用方式和效果。
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。