Docker有两种镜像生成的方式,供我们在打包自己的环境时选择,前面的知识点都提到过,分别是commitDockerFile build,两者的区别就在于commit使用自己已有的容器生成,而DockerFile是直接通过操作基础镜像来生成,中间没有容器这一角色的参与,通俗的理解如果你要生成的镜像是个人高定制化的你就在已有容器中commit,如果你只是想生成一个基础镜像组成的,对你来说没有个人属性或者定制化不高的镜像那就用DockerFile。

DockerFile是Docker提供的一种镜像生成的描述方式,它有自己的格式和规范,本章我们就介绍一下如何去写它。

首先还是要强调你要去github上看一眼DockerFile有什么,看不懂没关系,主要是有个映像,这里考虑到有的朋友github慢出天际,或者不知道到哪里找,我就随便找了一个让大家直接看一下。当然我找了一个简洁的,对于DockerFile整体来说肯定不全,但只要知道DockerFile啥样子就行。

这里说明一点,一定要多看官方的DockerFile,这样做的核心意义在于,当你要基于某一个别人的镜像为基础镜像时,dockerfile中可以给你提空很多关键的细节问题,最直接的一个例子就比方说你选择了一个别人开发的webapp,你觉得别开发的不完美,你开发了一个补丁,这个时候问题就来了,你不知道人家怎么启动的,你就要去dockerfile中找

#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM eclipse-temurin:11-jre-jammy

ENV CATALINA_HOME /usr/local/tomcat
ENV PATH $CATALINA_HOME/bin:$PATH
RUN mkdir -p "$CATALINA_HOME"
WORKDIR $CATALINA_HOME

# let "Tomcat Native" live somewhere isolated
ENV TOMCAT_NATIVE_LIBDIR $CATALINA_HOME/native-jni-lib
ENV LD_LIBRARY_PATH ${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$TOMCAT_NATIVE_LIBDIR

# see https://www.apache.org/dist/tomcat/tomcat-11/KEYS
# see also "versions.sh" (https://github.com/docker-library/tomcat/blob/master/versions.sh)
ENV GPG_KEYS A9C5DF4D22E99998D9875A5110C01C5A2F6059E7

ENV TOMCAT_MAJOR 11
ENV TOMCAT_VERSION 11.0.0-M1
ENV TOMCAT_SHA512 c2d02f2ac1b11122293af7d5ef3cf4dbc1932f19806ba89ea0cd36efcca7701304eef2f3cc03a496fd3a107080a582d3ca3faee207f0bce8b98c266f0a73edc7

COPY --from=tomcat:11.0.0-M1-jdk11-temurin-jammy $CATALINA_HOME $CATALINA_HOME
RUN set -eux; \
	apt-get update; \
	xargs -rt apt-get install -y --no-install-recommends < "$TOMCAT_NATIVE_LIBDIR/.dependencies.txt"; \
	rm -rf /var/lib/apt/lists/*

# verify Tomcat Native is working properly
RUN set -eux; \
	nativeLines="$(catalina.sh configtest 2>&1)"; \
	nativeLines="$(echo "$nativeLines" | grep 'Apache Tomcat Native')"; \
	nativeLines="$(echo "$nativeLines" | sort -u)"; \
	if ! echo "$nativeLines" | grep -E 'INFO: Loaded( APR based)? Apache Tomcat Native library' >&2; then \
		echo >&2 "$nativeLines"; \
		exit 1; \
	fi

EXPOSE 8080
CMD ["catalina.sh", "run"]

首先要说一下DockerFile的基础知识:

  1. 每条指令关键字本身都必须大写且一旦使用指令至少要跟随一个参数,就是说不允许存在其他脚本语言那样的不起实际作用的代码
  2. 整个DockerFile从上到下顺序执行
  3. #表示注释
  4. 每条指令都意味着会生成一个新的镜像层

我们还要知道,一个DockerFile在被用来构建镜像的时候,都发生了什么:

  1. docker从基础镜像运行一个容器
  2. 执行一条指令并对容器作出修改
  3. 执行类似docker commit的操作提交一个新的镜像层
  4. docker再基于刚提交的镜像运行一个新容器
  5. 执行dockerfile中的下一条指令直到所有指令都执行完成,形参最终镜像

现在再来看,DockerFile中的指令,随便说一下,它官方叫“保留字指令”

docker 安装git服务端 dockerfile git_docker 安装git服务端


FROM:你要生成的镜像,它的基础镜像是谁。

MAINTAINER:镜像的作者信息。

RUN:针对你生成的镜像需要运行某个命令时使用,值的格式支持shell格式和数组格式(此格式下0位是命令,其他位是参数)。

EXPOSE:声明镜像向外暴露的端口。

WORKDIR:指定该镜像下所有容器的入口路径,就是连接容器后所在的默认路径。

ENV:给镜像设置环境变量。

ADD:在构建镜像的时候,将某个包打进镜像中,同时把该包解压

COPY:在构建镜像的时候,将某个包打进镜像中,不会解压,值的格式有shell格式和数组格式两种。

VOLUME:设置数据卷默认接口路径,值的格式有shell格式和数组格式两种。

CMD:设置该镜像下容器被启动时所要运行的命令,它会被run命令的参数替换,比如特权方式进入,值的格式有shell格式、数组格式(此格式下0位是命令,其他位是参数)、以及当ENTRPOINT存在时用CMD来包裹参数,如果ENTRPOINT不存在,则第一位是命令,其他位是参数。

ENTRYPOINT:设置该镜像下容器被启动时所要运行的命令,它和CMD不一样的地方在于,如果它个CMD同时存在,则CMD中的所有值都将以ENTRYPOINT所运行命令的参数存在。

ONBUILD:当前你生成的镜像,被作为基础镜像的时候,触发执行。

USER:用处不大,基本用不到,作用是限制谁能使用这个DockerFile,但在工作中都是谁写的谁用,不会给其他人。

.dockerignore:同样是基本用不到,它是用来设置构建镜像的时候忽略什么东西。

应当注意的是:CMD和ENTRYPOINT都是容器启动时运行的命令,一般情况下我们随着我们镜像的目的而变化,比如是一个linuxos镜像,则运行/bin/bash,目的是打开为终端时用bash解析器接收操作命令,而如果是tomcat,则可以直接运行tomcat启动命令。但是有一个注意点它们各自都可以有多个,但当容器启动时永远生效的是最后一个,所以尽量不要无意义的写多个,因为虽然不生效,但是会生成中间层,你写的多了,意味着最终镜像大小也会不一样。


如果大家有兴趣看官方或者其他git上的DockerFile会常常发现,99%的镜像是基于scratch,这个镜像的地位就和JAVA的Objeck类似,它是一个公认的接近裸内核的Base镜像,但是你想基于它构建镜像,就意味着你要自己准备其他运行所需文件,比如你去看centos的官方DockerFile,你会发现它就是基于scratch镜像,ADD了一个centos-7-docker.tar.xz的包。国内我不推荐你这样用,因为很多tar包国内找不到资源,就算有也要钱的,比较恶心,尽量是找一个接近你目标的基础镜像就行了。

上面所属DockerFile的东西就没什么了,下面主要是给大家提供一下案例,更好的理解DockerFile的使用。

案例一:构建一个自己的CentOS

用过阿里云的centos-docker镜像的,会发现内核被精简到很多命令都没有,我们这个案例就是让我们自己的CentOS镜像拥有vim、ifconfig两个命令

FROM centos:7.8.2003
ENV mypath /tmp
WORKDIR $mypath
RUN yum -y install vim net-tools
EXPOSE 80 8080
CMD echo "OK!!!!"
CMD /bin/bash
[root@hdp3 wy] docker build -f ./mycentos -t mycentos:1.1 .
Sending build context to Docker daemon  36.28MB
Step 1/7 : FROM centos:7.8.2003
7.8.2003: Pulling from library/centos
9b4ebb48de8d: Pull complete 
Digest: sha256:8540a199ad51c6b7b51492fa9fee27549fd11b3bb913e888ab2ccf77cbb72cc1
Status: Downloaded newer image for centos:7.8.2003
 ---> afb6fca791e0
Step 2/7 : ENV mypath /tmp
 ---> Running in 7e5d5710bc0e
Removing intermediate container 7e5d5710bc0e
 ---> df69ee9ad5b0
Step 3/7 : WORKDIR $mypath
 ---> Running in 8f1e4f62c53d
Removing intermediate container 8f1e4f62c53d
 ---> 58ee7f5e3d8b
Step 4/7 : RUN yum -y install vim net-tools
 ---> Running in 3082e1dbed8c
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.bupt.edu.cn
 * extras: mirrors.bupt.edu.cn
 * updates: mirrors.bupt.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
---> Package vim-enhanced.x86_64 2:7.4.629-8.el7_9 will be installed
--> Processing Dependency: vim-common = 2:7.4.629-8.el7_9 for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: which for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: perl(:MODULE_COMPAT_5.16.3) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: libperl.so()(64bit) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: libgpm.so.2()(64bit) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Running transaction check
---> Package gpm-libs.x86_64 0:1.20.7-6.el7 will be installed
---> Package perl.x86_64 4:5.16.3-299.el7_9 will be installed
--> Processing Dependency: perl(Socket) >= 1.3 for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Scalar::Util) >= 1.10 for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl-macros for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(threads::shared) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(threads) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(constant) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Time::Local) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Time::HiRes) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Storable) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Socket) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Scalar::Util) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Pod::Simple::XHTML) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Pod::Simple::Search) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Getopt::Long) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Filter::Util::Call) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Temp) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Spec::Unix) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Spec::Functions) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Spec) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Path) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Exporter) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Cwd) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Carp) for package: 4:perl-5.16.3-299.el7_9.x86_64
---> Package perl-libs.x86_64 4:5.16.3-299.el7_9 will be installed
---> Package vim-common.x86_64 2:7.4.629-8.el7_9 will be installed
--> Processing Dependency: vim-filesystem for package: 2:vim-common-7.4.629-8.el7_9.x86_64
---> Package which.x86_64 0:2.20-7.el7 will be installed
--> Running transaction check
---> Package perl-Carp.noarch 0:1.26-244.el7 will be installed
---> Package perl-Exporter.noarch 0:5.68-3.el7 will be installed
---> Package perl-File-Path.noarch 0:2.09-2.el7 will be installed
---> Package perl-File-Temp.noarch 0:0.23.01-3.el7 will be installed
---> Package perl-Filter.x86_64 0:1.49-3.el7 will be installed
---> Package perl-Getopt-Long.noarch 0:2.40-3.el7 will be installed
--> Processing Dependency: perl(Pod::Usage) >= 1.14 for package: perl-Getopt-Long-2.40-3.el7.noarch
--> Processing Dependency: perl(Text::ParseWords) for package: perl-Getopt-Long-2.40-3.el7.noarch
---> Package perl-PathTools.x86_64 0:3.40-5.el7 will be installed
---> Package perl-Pod-Simple.noarch 1:3.28-4.el7 will be installed
--> Processing Dependency: perl(Pod::Escapes) >= 1.04 for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
--> Processing Dependency: perl(Encode) for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
---> Package perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 will be installed
---> Package perl-Socket.x86_64 0:2.010-5.el7 will be installed
---> Package perl-Storable.x86_64 0:2.45-3.el7 will be installed
---> Package perl-Time-HiRes.x86_64 4:1.9725-3.el7 will be installed
---> Package perl-Time-Local.noarch 0:1.2300-2.el7 will be installed
---> Package perl-constant.noarch 0:1.27-2.el7 will be installed
---> Package perl-macros.x86_64 4:5.16.3-299.el7_9 will be installed
---> Package perl-threads.x86_64 0:1.87-4.el7 will be installed
---> Package perl-threads-shared.x86_64 0:1.43-6.el7 will be installed
---> Package vim-filesystem.x86_64 2:7.4.629-8.el7_9 will be installed
--> Running transaction check
---> Package perl-Encode.x86_64 0:2.51-7.el7 will be installed
---> Package perl-Pod-Escapes.noarch 1:1.04-299.el7_9 will be installed
---> Package perl-Pod-Usage.noarch 0:1.63-3.el7 will be installed
--> Processing Dependency: perl(Pod::Text) >= 3.15 for package: perl-Pod-Usage-1.63-3.el7.noarch
--> Processing Dependency: perl-Pod-Perldoc for package: perl-Pod-Usage-1.63-3.el7.noarch
---> Package perl-Text-ParseWords.noarch 0:3.29-4.el7 will be installed
--> Running transaction check
---> Package perl-Pod-Perldoc.noarch 0:3.20-4.el7 will be installed
--> Processing Dependency: perl(parent) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
--> Processing Dependency: perl(HTTP::Tiny) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
--> Processing Dependency: groff-base for package: perl-Pod-Perldoc-3.20-4.el7.noarch
---> Package perl-podlators.noarch 0:2.5.1-3.el7 will be installed
--> Running transaction check
---> Package groff-base.x86_64 0:1.22.2-8.el7 will be installed
---> Package perl-HTTP-Tiny.noarch 0:0.033-3.el7 will be installed
---> Package perl-parent.noarch 1:0.225-244.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                  Arch     Version                      Repository
                                                                           Size
================================================================================
Installing:
 net-tools                x86_64   2.0-0.25.20131004git.el7     base      306 k
 vim-enhanced             x86_64   2:7.4.629-8.el7_9            updates   1.1 M
Installing for dependencies:
 gpm-libs                 x86_64   1.20.7-6.el7                 base       32 k
 groff-base               x86_64   1.22.2-8.el7                 base      942 k
 perl                     x86_64   4:5.16.3-299.el7_9           updates   8.0 M
 perl-Carp                noarch   1.26-244.el7                 base       19 k
 perl-Encode              x86_64   2.51-7.el7                   base      1.5 M
 perl-Exporter            noarch   5.68-3.el7                   base       28 k
 perl-File-Path           noarch   2.09-2.el7                   base       26 k
 perl-File-Temp           noarch   0.23.01-3.el7                base       56 k
 perl-Filter              x86_64   1.49-3.el7                   base       76 k
 perl-Getopt-Long         noarch   2.40-3.el7                   base       56 k
 perl-HTTP-Tiny           noarch   0.033-3.el7                  base       38 k
 perl-PathTools           x86_64   3.40-5.el7                   base       82 k
 perl-Pod-Escapes         noarch   1:1.04-299.el7_9             updates    52 k
 perl-Pod-Perldoc         noarch   3.20-4.el7                   base       87 k
 perl-Pod-Simple          noarch   1:3.28-4.el7                 base      216 k
 perl-Pod-Usage           noarch   1.63-3.el7                   base       27 k
 perl-Scalar-List-Utils   x86_64   1.27-248.el7                 base       36 k
 perl-Socket              x86_64   2.010-5.el7                  base       49 k
 perl-Storable            x86_64   2.45-3.el7                   base       77 k
 perl-Text-ParseWords     noarch   3.29-4.el7                   base       14 k
 perl-Time-HiRes          x86_64   4:1.9725-3.el7               base       45 k
 perl-Time-Local          noarch   1.2300-2.el7                 base       24 k
 perl-constant            noarch   1.27-2.el7                   base       19 k
 perl-libs                x86_64   4:5.16.3-299.el7_9           updates   690 k
 perl-macros              x86_64   4:5.16.3-299.el7_9           updates    44 k
 perl-parent              noarch   1:0.225-244.el7              base       12 k
 perl-podlators           noarch   2.5.1-3.el7                  base      112 k
 perl-threads             x86_64   1.87-4.el7                   base       49 k
 perl-threads-shared      x86_64   1.43-6.el7                   base       39 k
 vim-common               x86_64   2:7.4.629-8.el7_9            updates   5.9 M
 vim-filesystem           x86_64   2:7.4.629-8.el7_9            updates    11 k
 which                    x86_64   2.20-7.el7                   base       41 k

Transaction Summary
================================================================================
Install  2 Packages (+32 Dependent packages)

Total download size: 20 M
Installed size: 64 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/gpm-libs-1.20.7-6.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for gpm-libs-1.20.7-6.el7.x86_64.rpm is not installed
Public key for perl-Pod-Escapes-1.04-299.el7_9.noarch.rpm is not installed
--------------------------------------------------------------------------------
Total                                              9.4 MB/s |  20 MB  00:02     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-8.2003.0.el7.centos.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : gpm-libs-1.20.7-6.el7.x86_64                                1/34 
  Installing : 2:vim-filesystem-7.4.629-8.el7_9.x86_64                     2/34 
  Installing : 2:vim-common-7.4.629-8.el7_9.x86_64                         3/34 
  Installing : which-2.20-7.el7.x86_64                                     4/34 
install-info: No such file or directory for /usr/share/info/which.info.gz
  Installing : groff-base-1.22.2-8.el7.x86_64                              5/34 
  Installing : 1:perl-parent-0.225-244.el7.noarch                          6/34 
  Installing : perl-HTTP-Tiny-0.033-3.el7.noarch                           7/34 
  Installing : perl-podlators-2.5.1-3.el7.noarch                           8/34 
  Installing : perl-Pod-Perldoc-3.20-4.el7.noarch                          9/34 
  Installing : 1:perl-Pod-Escapes-1.04-299.el7_9.noarch                   10/34 
  Installing : perl-Encode-2.51-7.el7.x86_64                              11/34 
  Installing : perl-Text-ParseWords-3.29-4.el7.noarch                     12/34 
  Installing : perl-Pod-Usage-1.63-3.el7.noarch                           13/34 
  Installing : 4:perl-macros-5.16.3-299.el7_9.x86_64                      14/34 
  Installing : perl-Storable-2.45-3.el7.x86_64                            15/34 
  Installing : perl-Exporter-5.68-3.el7.noarch                            16/34 
  Installing : perl-constant-1.27-2.el7.noarch                            17/34 
  Installing : perl-Socket-2.010-5.el7.x86_64                             18/34 
  Installing : perl-Time-Local-1.2300-2.el7.noarch                        19/34 
  Installing : perl-Carp-1.26-244.el7.noarch                              20/34 
  Installing : perl-PathTools-3.40-5.el7.x86_64                           21/34 
  Installing : perl-Scalar-List-Utils-1.27-248.el7.x86_64                 22/34 
  Installing : 1:perl-Pod-Simple-3.28-4.el7.noarch                        23/34 
  Installing : perl-File-Temp-0.23.01-3.el7.noarch                        24/34 
  Installing : perl-File-Path-2.09-2.el7.noarch                           25/34 
  Installing : perl-threads-shared-1.43-6.el7.x86_64                      26/34 
  Installing : perl-threads-1.87-4.el7.x86_64                             27/34 
  Installing : 4:perl-Time-HiRes-1.9725-3.el7.x86_64                      28/34 
  Installing : perl-Filter-1.49-3.el7.x86_64                              29/34 
  Installing : 4:perl-libs-5.16.3-299.el7_9.x86_64                        30/34 
  Installing : perl-Getopt-Long-2.40-3.el7.noarch                         31/34 
  Installing : 4:perl-5.16.3-299.el7_9.x86_64                             32/34 
  Installing : 2:vim-enhanced-7.4.629-8.el7_9.x86_64                      33/34 
  Installing : net-tools-2.0-0.25.20131004git.el7.x86_64                  34/34 
  Verifying  : perl-HTTP-Tiny-0.033-3.el7.noarch                           1/34 
  Verifying  : perl-threads-shared-1.43-6.el7.x86_64                       2/34 
  Verifying  : perl-Storable-2.45-3.el7.x86_64                             3/34 
  Verifying  : groff-base-1.22.2-8.el7.x86_64                              4/34 
  Verifying  : perl-Exporter-5.68-3.el7.noarch                             5/34 
  Verifying  : perl-constant-1.27-2.el7.noarch                             6/34 
  Verifying  : perl-PathTools-3.40-5.el7.x86_64                            7/34 
  Verifying  : 4:perl-macros-5.16.3-299.el7_9.x86_64                       8/34 
  Verifying  : 2:vim-enhanced-7.4.629-8.el7_9.x86_64                       9/34 
  Verifying  : 1:perl-parent-0.225-244.el7.noarch                         10/34 
  Verifying  : perl-Socket-2.010-5.el7.x86_64                             11/34 
  Verifying  : which-2.20-7.el7.x86_64                                    12/34 
  Verifying  : 2:vim-filesystem-7.4.629-8.el7_9.x86_64                    13/34 
  Verifying  : perl-File-Temp-0.23.01-3.el7.noarch                        14/34 
  Verifying  : net-tools-2.0-0.25.20131004git.el7.x86_64                  15/34 
  Verifying  : 1:perl-Pod-Simple-3.28-4.el7.noarch                        16/34 
  Verifying  : perl-Time-Local-1.2300-2.el7.noarch                        17/34 
  Verifying  : 1:perl-Pod-Escapes-1.04-299.el7_9.noarch                   18/34 
  Verifying  : perl-Carp-1.26-244.el7.noarch                              19/34 
  Verifying  : 2:vim-common-7.4.629-8.el7_9.x86_64                        20/34 
  Verifying  : perl-Scalar-List-Utils-1.27-248.el7.x86_64                 21/34 
  Verifying  : perl-Pod-Usage-1.63-3.el7.noarch                           22/34 
  Verifying  : perl-Encode-2.51-7.el7.x86_64                              23/34 
  Verifying  : perl-Pod-Perldoc-3.20-4.el7.noarch                         24/34 
  Verifying  : perl-podlators-2.5.1-3.el7.noarch                          25/34 
  Verifying  : 4:perl-5.16.3-299.el7_9.x86_64                             26/34 
  Verifying  : perl-File-Path-2.09-2.el7.noarch                           27/34 
  Verifying  : perl-threads-1.87-4.el7.x86_64                             28/34 
  Verifying  : 4:perl-Time-HiRes-1.9725-3.el7.x86_64                      29/34 
  Verifying  : gpm-libs-1.20.7-6.el7.x86_64                               30/34 
  Verifying  : perl-Filter-1.49-3.el7.x86_64                              31/34 
  Verifying  : perl-Getopt-Long-2.40-3.el7.noarch                         32/34 
  Verifying  : perl-Text-ParseWords-3.29-4.el7.noarch                     33/34 
  Verifying  : 4:perl-libs-5.16.3-299.el7_9.x86_64                        34/34 

Installed:
  net-tools.x86_64 0:2.0-0.25.20131004git.el7                                   
  vim-enhanced.x86_64 2:7.4.629-8.el7_9                                         

Dependency Installed:
  gpm-libs.x86_64 0:1.20.7-6.el7                                                
  groff-base.x86_64 0:1.22.2-8.el7                                              
  perl.x86_64 4:5.16.3-299.el7_9                                                
  perl-Carp.noarch 0:1.26-244.el7                                               
  perl-Encode.x86_64 0:2.51-7.el7                                               
  perl-Exporter.noarch 0:5.68-3.el7                                             
  perl-File-Path.noarch 0:2.09-2.el7                                            
  perl-File-Temp.noarch 0:0.23.01-3.el7                                         
  perl-Filter.x86_64 0:1.49-3.el7                                               
  perl-Getopt-Long.noarch 0:2.40-3.el7                                          
  perl-HTTP-Tiny.noarch 0:0.033-3.el7                                           
  perl-PathTools.x86_64 0:3.40-5.el7                                            
  perl-Pod-Escapes.noarch 1:1.04-299.el7_9                                      
  perl-Pod-Perldoc.noarch 0:3.20-4.el7                                          
  perl-Pod-Simple.noarch 1:3.28-4.el7                                           
  perl-Pod-Usage.noarch 0:1.63-3.el7                                            
  perl-Scalar-List-Utils.x86_64 0:1.27-248.el7                                  
  perl-Socket.x86_64 0:2.010-5.el7                                              
  perl-Storable.x86_64 0:2.45-3.el7                                             
  perl-Text-ParseWords.noarch 0:3.29-4.el7                                      
  perl-Time-HiRes.x86_64 4:1.9725-3.el7                                         
  perl-Time-Local.noarch 0:1.2300-2.el7                                         
  perl-constant.noarch 0:1.27-2.el7                                             
  perl-libs.x86_64 4:5.16.3-299.el7_9                                           
  perl-macros.x86_64 4:5.16.3-299.el7_9                                         
  perl-parent.noarch 1:0.225-244.el7                                            
  perl-podlators.noarch 0:2.5.1-3.el7                                           
  perl-threads.x86_64 0:1.87-4.el7                                              
  perl-threads-shared.x86_64 0:1.43-6.el7                                       
  vim-common.x86_64 2:7.4.629-8.el7_9                                           
  vim-filesystem.x86_64 2:7.4.629-8.el7_9                                       
  which.x86_64 0:2.20-7.el7                                                     

Complete!
Removing intermediate container 3082e1dbed8c
 ---> 3fa06ced902a
Step 5/7 : EXPOSE 80 8080
 ---> Running in feddd97d4cf1
Removing intermediate container feddd97d4cf1
 ---> 30e6aaba5cc8
Step 6/7 : CMD echo "OK!!!!"
 ---> Running in 3e8aa0c7b50c
Removing intermediate container 3e8aa0c7b50c
 ---> 287222d0c6d5
Step 7/7 : CMD /bin/bash
 ---> Running in 01af15ed4714
Removing intermediate container 01af15ed4714
 ---> e33020478f31
Successfully built e33020478f31
Successfully tagged mycentos:1.1

[root@hdp3 wy] docker run -it mycentos
[root@d95fa13e13c4 tmp] ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 6  bytes 508 (508.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

案例二:做一个curl容器

上面说了CMD和ENTRYPOINT都是容器开启运行的命令,但我想坑定有人不太理解什么叫CMD会被覆盖,而ENTRYPOINT是拼接,我这里就用案例一做出的镜像了

FROM mycentos:1.1
RUN yum install -y curl
CMD ["curl" , "-s" , "http://ip.cn"]
[root@hdp3 wy]# docker build -f ./mycurl -t mycurl:1.1 .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM mycentos:1.1
 ---> e33020478f31
Step 2/3 : RUN yum install -y curl
 ---> Running in 9e1a4d73823f
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.bupt.edu.cn
 * extras: mirrors.bupt.edu.cn
 * updates: mirrors.bupt.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package curl.x86_64 0:7.29.0-57.el7 will be updated
---> Package curl.x86_64 0:7.29.0-59.el7_9.1 will be an update
--> Processing Dependency: libcurl = 7.29.0-59.el7_9.1 for package: curl-7.29.0-59.el7_9.1.x86_64
--> Running transaction check
---> Package libcurl.x86_64 0:7.29.0-57.el7 will be updated
---> Package libcurl.x86_64 0:7.29.0-59.el7_9.1 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch           Version                   Repository       Size
================================================================================
Updating:
 curl            x86_64         7.29.0-59.el7_9.1         updates         271 k
Updating for dependencies:
 libcurl         x86_64         7.29.0-59.el7_9.1         updates         223 k

Transaction Summary
================================================================================
Upgrade  1 Package (+1 Dependent package)

Total download size: 494 k
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
--------------------------------------------------------------------------------
Total                                               87 kB/s | 494 kB  00:05     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : libcurl-7.29.0-59.el7_9.1.x86_64                             1/4 
  Updating   : curl-7.29.0-59.el7_9.1.x86_64                                2/4 
  Cleanup    : curl-7.29.0-57.el7.x86_64                                    3/4 
  Cleanup    : libcurl-7.29.0-57.el7.x86_64                                 4/4 
  Verifying  : curl-7.29.0-59.el7_9.1.x86_64                                1/4 
  Verifying  : libcurl-7.29.0-59.el7_9.1.x86_64                             2/4 
  Verifying  : curl-7.29.0-57.el7.x86_64                                    3/4 
  Verifying  : libcurl-7.29.0-57.el7.x86_64                                 4/4 

Updated:
  curl.x86_64 0:7.29.0-59.el7_9.1                                               

Dependency Updated:
  libcurl.x86_64 0:7.29.0-59.el7_9.1                                            

Complete!
Removing intermediate container 9e1a4d73823f
 ---> 41546ea69929
Step 3/3 : CMD ["curl" , "-s" , "http://ip.cn"]
 ---> Running in 1f99e47bd3ad
Removing intermediate container 1f99e47bd3ad
 ---> 46521b3a93e3
Successfully built 46521b3a93e3
Successfully tagged mycurl:1.1
[root@hdp3 wy]# docker run -it mycurl:1.1
[root@hdp3 wy]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@hdp3 wy]# docker run mycurl:1.1
[root@hdp3 wy]# docker run -it mycurl:1.1 /bin/bash
[root@b606e2267769 tmp]#

用过curl的应该知道正常情况下,应该输出curl的结果,但是由于我在后面写了命令,覆盖了-s的命令,所以成了普通的os容器

此时如果再换一下DockerFile

FROM mycentos:1.1
RUN yum install -y curl
ENTRYPOINT curl -s http://ip.cn

通过命令可以发现并没有被覆盖,而是追加

[root@hdp3 wy]# docker build -f ./mycurl -t mycurl:2.1 .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM mycentos:1.1
 ---> e33020478f31
Step 2/3 : RUN yum install -y curl
 ---> Using cache
 ---> 41546ea69929
Step 3/3 : ENTRYPOINT curl -s http://ip.cn
 ---> Running in 6cce24c0bdc6
Removing intermediate container 6cce24c0bdc6
 ---> ca77d4a27ff0
Successfully built ca77d4a27ff0
Successfully tagged mycurl:2.1
[root@hdp3 wy] docker run mycurl:2.1 -i
[root@hdp3 wy] docker ps -a --no-trunc
CONTAINER ID                                                       IMAGE        COMMAND                                  CREATED         STATUS                          PORTS     NAMES
f8ab9da3b2c661b73f4d77c7adf0fa8666b1dc3693cf77afb830fcd99fbc44ac   mycurl:2.1   "/bin/sh -c 'curl -s http://ip.cn' -i"   2 minutes ago   Exited (0) About a minute ago             competent_pare

案例三:ONBUILD

在别人继承我们的镜像时输出一句话

FROM mycurl:2.1
ONBUILD RUN echo "作者真帅"

通过构建日志可以发现并没有特别

[root@hdp3 wy]# docker build -f ./mycurl -t mycurl:3.1 .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM mycurl:2.1
 ---> ca77d4a27ff0
Step 2/2 : ONBUILD RUN echo "作者真帅"
 ---> Running in 8473ecef9d28
Removing intermediate container 8473ecef9d28
 ---> 72a85803bd8c
Successfully built 72a85803bd8c
Successfully tagged mycurl:3.1

但是此时我们在构建一个4.1版本,继承3.1,看一下会发生什么

FROM mycurl:3.1

什么不干就只继承,可以发现会触发基础镜像的ONBUILD

[root@hdp3 wy] docker build -f ./mycurl -t mycurl:4.1 .
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM mycurl:3.1
# Executing 1 build trigger
 ---> Running in bc935c5b944c
作者真帅
Removing intermediate container bc935c5b944c
 ---> a3eb036e1993
Successfully built a3eb036e1993
Successfully tagged mycurl:4.1

案例四:完整的串一遍

FROM centos:7.8.2003
MAINTAINER wy<123456@163.com>
COPY /opt/test.txt /opt
ADD /opt/jdk-8u171-linux-x64.tar.gz /opt
RUN yum install -y vim
ENV MYPATH /opt
WORKDIR $MYPATH
ENV JAVA_HOME /opt/jdk1.8.0_171
NEV CLASSPATH ${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
ENV PATH ${PATH}:${JAVA_HOME}/bin
EXPOSE 8080
CMD source /etc/profile && java -version