0

『现学现忘』Docker基础 — 37、ONBUILD指令介绍 - 繁华似锦Fighting

 2 years ago
source link: https://www.cnblogs.com/liuyuelinfighting/p/16085775.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1、ONBUILD指令说明

ONBUILD是一个特殊的指令,它后面跟的是其它指令,比如 RUN, COPY 等,而这些指令在当前镜像构建时并不会被执行。以当前镜像为基础镜像,去构建下一级镜像的时候,才会执行ONBUILD后面的指令。

Dockerfile中的其它指令,都是为了定制当前镜像而准备的,唯有ONBUILD指令是为了帮助别人而定制自己,来准备的。

即:ONBUILD指令在构建一个有继承的Dockerfile文件时,父镜像在被子镜像继承后,父镜像的ONBUILD指令被触发(触发指令,类似一个触发器)。

2、演示ONBUILD指令的使用

(1)演示准备

我们创建一个Dockerfile文件:Dockerfile_onbuild

shell
[root@192 mydockerfile]# pwd
/home/mydockerfile
[root@192 mydockerfile]# touch Dockerfile_onbuild

编辑Dockerfile_onbuild文件内容如下:

dockerfile
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镜像,如下:

shell
[root@192 mydockerfile]# docker build -f /home/mydockerfile/Dockerfile_onbuild -t onbuild/father:1.0 .
Sending build context to Docker daemon   5.12kB
Step 1/4 : FROM centos
 ---> 300e315adb2f
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) <[email protected]>"
 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

shell
[root@192 mydockerfile]# pwd
/home/mydockerfile
[root@192 mydockerfile]# touch Dockerfile_onbuild_son

编辑Dockerfile_onbuild_son文件内容如下:

dockerfile
# 这里要继承上面生成的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镜像。

如下:(请看下文中的注释)

shell
[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指令的使用方式和效果。

3、补充:crul命令解释

  • curl命令可以用来执行下载、发送各种HTTP请求,指定HTTP头部等操作。
  • 如果系统没有curl命令可以使用yum install curl命令安装,也可以下载安装。
  • curl是将下载文件输出到stdout(标准输出,也就是控制台中)。
    例如:
    使用命令:curl http://www.baidu.com
    执行后,www.baidu.com的HTML页面内容,就会显示在屏幕上了。
    这是最简单的使用方法,用这个命令获得了URL所指向的页面。
  • 同样,如果这里的URL指向的是一个文件或者一幅图,都可以直接下载到本地。如果下载的是HTML文档,那么将缺省响应头部的信息,即HTML文档的header。要想全部显示,请加参数- i

__EOF__


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK