第7章 Dockerfile详解

第7章 Dockerfile详解

7.1. 认识Dockerfile

7.1.1. 镜像的生成途径

  • 基于容器制作
  • dockerfile,docker build

7.1.2. Dockerfile介绍

Docker中有个非常重要的概念叫做—镜像(Image)。Docker镜像是一个特殊的文件系统,除了提供容器运行时所需要的程序、库、资源、配置等文件外,还包含了一些运行时准备的一些配置参数(如匿名卷、环境变量、用户等)。镜像不包含任何动态数据,其内容在构建之后也不会被改变。

镜像的定制实际上就是定制每一层所添加的配置、文件。如果我们可以把每一层修改、安装、构建、操作的命令都写入一个脚本,用这个脚本来构建、定制镜像,那么之前提及的无法重复的问题、镜像构建透明性的问题、体积的问题就都会解决。这个脚本就是dockerfile。

Dockerfile是一个文本文件,其内包含了一条条的指令(Instruction),每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建。

7.1.3. Dockerfile指令

  • FROM
  • MAINTAINER
  • COPY
  • ADD
  • WORKDIR
  • VOLUME
  • EXPOSE
  • ENV
  • RUN
  • CMD
  • ENTRYPOINT
  • HEALTHCHECK
  • ONBUILD
  • USER
  • ARG
  • SHELL
  • STOPSIGNAL

7.1.4. Dockerfile的使用

  • Dockerfile编写的基本结构
    Dockerfile一般分为四部分:基础镜像信息、维护者信息、镜像操作指令和容器启动时执行指令。“#”为dockerfile中的注释。

  • 一台主机可以有多个Dockerfile
    要使用多个Dockerfile创建镜像,可以在不同目录编写Dockerfile,然后在Dockerfile所在的目录下构建新的镜像;
    注意:Dockerfile中所包含的需要的内容:如copy的文件、目录等,都需要在dockerfile同级目录下存在;

  • Docker build 基于dockerfile制作镜像的命令
    格式:docker build [OPTIONS] PATH | URL | -
    选项:
    -t # 打标签
    -c # -cpu-shares int :CPU份额(相对权重)
    -m # -memory bytes :内存限制
    --build-arg # 设置构建时变量,就是构建的时候修改ARG指令的参数

7.2. FROM指令

介绍:

  • FROM指令必须时dockerfile中非注释的第一个指令,即一个dockerfile从FROM语句开始;
  • FROM指令用于为镜像文件构建过程指定基础镜像,后续的指令运行于此基础镜像所提供的运行环境;
  • 实践中,基础镜像可以时任何可用镜像文件,默认情况下,docker build会在docker主机上查找指定的镜像文件,在其不存在时,则会自动从docker公共库pull镜像下来。如果找不到指定的镜像文件,docker build会返回一个错误信息;
  • FROM可以在一个dockerfile中多次出现;
  • 如果FROM没有指定镜像标签,则默认使用latest标签;

格式:

FROM <repository>[:<tag>] 或
FROM <repository>@<digest>

注:
	<repository>		# 指定作为base image的名称
	<tag>			# base image的标签,省略时默认latest
	<digest>			# 时镜像的哈希码;使用哈希码会更安全一点

示例:

FROM busybox:latest

7.3. MAINTAINER

介绍:

  • 用于让dockerfile制作者提供本人的详细信息;
  • Dockerfile并不限制MAINTAINER指令可在出现的位置,推荐放置于FROM指令之后;

格式:

MAINTAINER <AUTHTOR`S DETAIL>

示例:

FROM busybox:latest
MAINTAINER “Along <along@along.com>"

7.4. COPY

介绍:

  • 用于从docker主机复制新文件或者目录至创建的新镜像指定路径中

格式:

COPY <src> … <dest> 或
COPY [“<src>” … “<dest>”]

注:
	<src>		# 要复制的源文件或目录,支持使用通配符;
	<dest>		# 目标路径,即正在创建的image的文件系统路径;建议<dest>使用绝对路径

文件复制准则:

  • 必须时build上下文中的路径,不能是其父目录中的文件;
  • 如果是目录,则其内部文件或子目录会被递归复制;但目录自身不会被复制;
  • 如果指定了多个,或在中使用了通配符,则必须是一个目录,且必须以/结尾;
  • 如果事先不存在,他将会被自动创建,这包括父目录路径;

示例:
COPY文件

# 编写dockerfile文件
[root@localhost test]# cat dockerfile
FROM busybox:latest
MAINTAINER "Along <along@along.com>"
COPY index.html /data/web/html/

# 在dockerfile同级目录下准备index.html文件
[root@localhost test]# cat index.html
<h1>Busybox httpd server</h1>

# 使用build制作镜像
[root@localhost test]# docker build -t busyboxhttpd:v0.1 ./
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM busybox:latest
 ---> 6d5fcfe5ff17
Step 2/3 : MAINTAINER "Along <along@along.com>"
 ---> Using cache
 ---> c914e57b9397
Step 3/3 : COPY index.html /data/web/html/
 ---> Using cache
 ---> a08b0b454b41
Successfully built a08b0b454b41
Successfully tagged busyboxhttpd:v0.1

# 基于此新建镜像运行容器,进行验证	
# --rm 在容器关闭时,直接删除容器,方便实验;
[root@localhost test]# docker run --name we1 --rm busyboxhttpd:v0.1 cat /data/web/html/index.html
<h1>Busybox httpd server</h1>

COPY目录

# 编写dockerfile文件
[root@localhost test]# cat dockerfile
FROM busybox:latest
MAINTAINER "Along <along@along.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/

# 如果复制目录,则其内部文件或子目录会被递归复制,但<src>目录自身不会被复制;需要把复制目录名字也卸载容器中要复制的路径下;
# 在dockerfile同级目录下准备好yum.repos.d目录
[root@localhost test]# cp -r /etc/yum.repos.d/ ./
[root@localhost test]# ls yum.repos.d/
CentOS-Base.repo  docker-ce.repo  epel.repo  epel-testing.repo

# 使用build制作镜像
[root@localhost test]# docker build -t busyboxhttpd:v0.2 ./
Sending build context to Docker daemon  13.82kB
Step 1/4 : FROM busybox:latest
 ---> 6d5fcfe5ff17
Step 2/4 : MAINTAINER "Along <along@along.com>"
 ---> Using cache
 ---> c914e57b9397
Step 3/4 : COPY index.html /data/web/html/
 ---> Using cache
 ---> a08b0b454b41
Step 4/4 : COPY yum.repos.d /etc/yum.repos.d/
 ---> 87a1a14a8b7e
Successfully built 87a1a14a8b7e
Successfully tagged busyboxhttpd:v0.2

# 基于此新建镜像运行容器,进行验证;
[root@localhost test]# docker run --name web1 --rm busyboxhttpd:v0.2 ls /etc/yum.repos.d/
CentOS-Base.repo
docker-ce.repo
epel-testing.repo
epel.repo

7.5. ADD

介绍:

  • ADD指令类似于COPY指令,ADD指令支持使用TAR文件和URL路径

格式:

ADD <src> … <dest> 或
ADD [“<src>”…”<dest>”]

操作准则:

  • 同COPY指令
  • 如果为URL且不以/结尾,则指定的文件将被下载并直接被创建为;如果以/结尾,则文件名URL指定的文件将被直接下载并保存为/;
  • 如果是一个本地文件系统上的压缩格式为tar文件,将被展开为一个目录,其行为类似于“tar -x“命令;然而,通过URL获取到的tar文件将不会自动展开;
  • 如果有多个,或其简介或直接使用了通配符,则必须是一个以/结尾的目录路径;如果不以/结尾,则其被视作一个普通文件,的内容将被直接写入到

示例:

# 编写dockerfile文件
# ADD的<src>是网上的nginx下载路径
[root@localhost test]# vim dockerfile
FROM busybox:latest
MAINTAINER "Along <along@along.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/

# 使用build制作镜像
[root@localhost test]# docker build -t busyboxhttpd:v0.3 ./
Sending build context to Docker daemon  13.82kB
Step 1/5 : FROM busybox:latest
 ---> 6d5fcfe5ff17
Step 2/5 : MAINTAINER "Along <along@along.com>"
 ---> Using cache
 ---> c914e57b9397
Step 3/5 : COPY index.html /data/web/html/
 ---> Using cache
 ---> a08b0b454b41
Step 4/5 : COPY yum.repos.d /etc/yum.repos.d/
 ---> Using cache
 ---> 87a1a14a8b7e
Step 5/5 : ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
Downloading  1.028MB/1.028MB
 ---> 82121b2c6666
Successfully built 82121b2c6666
Successfully tagged busyboxhttpd:v0.3

# 基于此新建镜像运行容器,进行验证;
[root@localhost test]# docker run --name web1 --rm busyboxhttpd:v0.3 ls /usr/local/src/
nginx-1.15.8.tar.gz

COPY本地的路径的tar包

# 编写dockerfile文件
[root@localhost test]# vim dockerfile
FROM busybox:latest
MAINTAINER "Along <along@along.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
# ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
ADD nginx-1.15.8.tar.gz /usr/local/src/

# 在dockerfile同级目录下准备好tar包
[root@localhost test]# wget http://nginx.org/download/nginx-1.15.8.tar.gz

# 使用build制作镜像;
[root@localhost test]# docker build -t busyboxhttpd:v0.4 ./
Sending build context to Docker daemon  1.042MB
Step 1/5 : FROM busybox:latest
 ---> 6d5fcfe5ff17
Step 2/5 : MAINTAINER "Along <along@along.com>"
 ---> Using cache
 ---> c914e57b9397
Step 3/5 : COPY index.html /data/web/html/
 ---> Using cache
 ---> a08b0b454b41
Step 4/5 : COPY yum.repos.d /etc/yum.repos.d/
 ---> Using cache
 ---> 87a1a14a8b7e
Step 5/5 : ADD nginx-1.15.8.tar.gz /usr/local/src/
 ---> db74b6915c8a
Successfully built db74b6915c8a
Successfully tagged busyboxhttpd:v0.4

# 基于此新建镜像运行容器,进行验证;
[root@localhost test]# docker run --name web1 --rm busyboxhttpd:v0.4 ls /usr/local/src/nginx-1.15.8
CHANGES
CHANGES.ru
LICENSE
README
auto
conf
configure
contrib
html
man
src

7.6. WORKDIR

介绍:

  • 用于为dockerfile中所有的RUN,CMD,ENTRYPOINT,COPY和ADD指定工作目录

格式:

WORKDIR <dirpath>
# 在dockerfile中,WORKDIR可以多次出现,其路径也可以为相对路径;相对路径是对此前一个WORKDIER指令指定的路径;
# WORKDIR也可以调用由ENV指定定义的变量;

示例:

# 编写dockerfile文件
[root@localhost test]# vim dockerfile
FROM busybox:latest
MAINTAINER "Along <along@along.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
# ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
WORKDIR /usr/local/
ADD nginx-1.15.8.tar.gz ./src/

# 使用build制作镜像
[root@localhost test]# docker build -t busyboxhttpd:v0.5 ./
Sending build context to Docker daemon  1.042MB
Step 1/6 : FROM busybox:latest
 ---> 6d5fcfe5ff17
Step 2/6 : MAINTAINER "Along <along@along.com>"
 ---> Using cache
 ---> c914e57b9397
Step 3/6 : COPY index.html /data/web/html/
 ---> Using cache
 ---> a08b0b454b41
Step 4/6 : COPY yum.repos.d /etc/yum.repos.d/
 ---> Using cache
 ---> 87a1a14a8b7e
Step 5/6 : WORKDIR /usr/local/
 ---> Running in 5e1ad05054f5
Removing intermediate container 5e1ad05054f5
 ---> 3e1f7fe54abe
Step 6/6 : ADD nginx-1.15.8.tar.gz ./src/
 ---> 0ffa387d305d
Successfully built 0ffa387d305d
Successfully tagged busyboxhttpd:v0.5

# 基于此新建镜像运行容器,进行验证
[root@localhost test]# docker run --name web1 --rm busyboxhttpd:v0.5 ls /usr/local/src/nginx-1.15.8
CHANGES
CHANGES.ru
LICENSE
README
auto
conf
configure
contrib
html
man
src

7.7. VOLUME

介绍:

  • 用于在image中创建一个挂载点目录,以挂载Docker host上的卷或其他容器的卷

格式:

VOLUME <mountpoint> 或
VOLUME [“<mountpoint>”]
# 注:如果挂载点目录路径下此前的文件存在,docker run命令会在卷挂载完成后将此前的所有文件复制到新挂载的卷中

示例:

# 编写dockerfile文件
[root@localhost test]# vim dockerfile
FROM busybox:latest
MAINTAINER "Along <along@along.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
# ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
WORKDIR /usr/local/
ADD nginx-1.15.8.tar.gz ./src/
VOLUME /data/mysql

# 使用build制作镜像
[root@localhost test]# docker build -t busyboxhttpd:v05 ./
Sending build context to Docker daemon  1.042MB
Step 1/7 : FROM busybox:latest
 ---> 6d5fcfe5ff17
Step 2/7 : MAINTAINER "Along <along@along.com>"
 ---> Using cache
 ---> c914e57b9397
Step 3/7 : COPY index.html /data/web/html/
 ---> Using cache
 ---> a08b0b454b41
Step 4/7 : COPY yum.repos.d /etc/yum.repos.d/
 ---> Using cache
 ---> 87a1a14a8b7e
Step 5/7 : WORKDIR /usr/local/
 ---> Using cache
 ---> 3e1f7fe54abe
Step 6/7 : ADD nginx-1.15.8.tar.gz ./src/
 ---> Using cache
 ---> 0ffa387d305d
Step 7/7 : VOLUME /data/mysql
 ---> Running in 322a17c5d4d7
Removing intermediate container 322a17c5d4d7
 ---> 6fc0a314989f
Successfully built 6fc0a314989f
Successfully tagged busyboxhttpd:v05

# 基于此新建镜像运行容器,进行验证
[root@localhost test]# docker run --name web1 --rm -it busyboxhttpd:v05 /bin/sh
/usr/local #
# 另外打开一个终端,查询存储卷
[root@localhost ~]# docker inspect -f {{.Mounts}} web1
[{volume b4ad1323108db2b6a65e798580d370bcc802bf460c9235ff0da3138cb98ab7f4 /var/lib/docker/volumes/b4ad1323108db2b6a65e798580d370bcc802bf460c9235ff0da3138cb98ab7f4/_data /data/mysql local  true }]

7.8. EXPOSE

介绍:

  • 用于为容器打开指定要监听的端口,以实现与外部通信

格式:

EXPOSE <port>[/ <protocal>] [<port>[/protocol>]..
注:
	<protocol>	# 用于指定传输层协议,可为tcp或udp;默认为TCP协议;
	EXPOSE		# 可一次指定多个端口,例如:EXPOSE 11211/udp 11211/tcp

示例:

# 编写dockerfile文件
[root@localhost test]# vim dockerfile
FROM busybox:latest
MAINTAINER "Along <along@along.com>"
COPY index.html /data/web/html/
COPY yum.repos.d /etc/yum.repos.d/
# ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
WORKDIR /usr/local/
ADD nginx-1.15.8.tar.gz ./src/
VOLUME /data/mysql
EXPOSE 80/tcp
~
# 使用build制作镜像
[root@localhost test]# docker build -t busyboxhttpd:v0.6 ./
Sending build context to Docker daemon  1.042MB
Step 1/8 : FROM busybox:latest
 ---> 6d5fcfe5ff17
Step 2/8 : MAINTAINER "Along <along@along.com>"
 ---> Using cache
 ---> c914e57b9397
Step 3/8 : COPY index.html /data/web/html/
 ---> Using cache
 ---> a08b0b454b41
Step 4/8 : COPY yum.repos.d /etc/yum.repos.d/
 ---> Using cache
 ---> 87a1a14a8b7e
Step 5/8 : WORKDIR /usr/local/
 ---> Using cache
 ---> 3e1f7fe54abe
Step 6/8 : ADD nginx-1.15.8.tar.gz ./src/
 ---> Using cache
 ---> 0ffa387d305d
Step 7/8 : VOLUME /data/mysql
 ---> Using cache
 ---> 6fc0a314989f
Step 8/8 : EXPOSE 80/tcp
 ---> Running in a311792a1053
Removing intermediate container a311792a1053
 ---> 9fce0c1fda61
Successfully built 9fce0c1fda61
Successfully tagged busyboxhttpd:v0.6

# 基于此新建镜像运行容器,进行验证
[root@localhost test]# docker run --name web1 -P --rm -it busyboxhttpd:v0.6 /bin/httpd -f -h /data/web/html/
# 另打开一个终端,验证httpd服务的80端口
[root@localhost ~]# docker inspect -f {{.NetworkSettings.IPAddress}} web1
172.17.0.2
[root@localhost ~]# curl 172.17.0.2
<h1>Busybox httpd server</h1>
# 在宿主机通过暴漏的端口访问httpd服务
[root@localhost ~]# docker port web1
80/tcp -> 0.0.0.0:32770
[root@localhost ~]# curl 127.0.0.1:32770
<h1>Busybox httpd server</h1>
# 就算dockerfile中有EXPOSE指令暴露端口,但是不是真正的暴漏;需要在启动容器时,使用-P选项真正的暴漏端口。

7.9. ENV

介绍:

  • 用于为镜像定义所需的环境变量,并可被dockerfile文件中位于其后的其他指令(如ENV,ADD,COPY等)所调用;
  • 调用格式为$variable_name 或${variable_name}

格式:

ENV <key> <value> 或
ENV <key>=<value>

注:
	第一种格式中,<key>之后的所有内容均被视为<value>的组成部分,因此,一次只能设置一个变量;
	第二种格式可以一次设置多个变量,每个变量为一个”<key>=<value>”的键值对;如果<value>中包含空格,可以用““进行转义,也可以通过对<value>加引号进行标识;另外,反斜线也可用于续行;
	定义多个变量时,建议使用第二种方式;

示例:

# 编写dockerfile文件
[root@localhost test]# vim dockerfile
FROM busybox:latest
MAINTAINER "Along <along@along.com>"
ENV DOC_ROOT=/data/web/html/ 
  WEB_SERVER_PACKAGE="nginx-1.15.8"

COPY index.html ${DOC_ROOT}
COPY yum.repos.d /etc/yum.repos.d/

# ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
WORKDIR /usr/local/
ADD ${WEB_SERVER_PACKAGE}.tar.gz ./src/
VOLUME /data/mysql
EXPOSE 8080:80/tcp

# 使用build制作镜像
[root@localhost test]# docker build -t busyboxhttpd:v0.7 ./
Sending build context to Docker daemon  1.042MB
Step 1/9 : FROM busybox:latest
 ---> 6d5fcfe5ff17
Step 2/9 : MAINTAINER "Along <along@along.com>"
 ---> Using cache
 ---> c914e57b9397
Step 3/9 : ENV DOC_ROOT=/data/web/html/   WEB_SERVER_PACKAGE="nginx-1.15.8"
 ---> Running in e44fb85dc9e8
Removing intermediate container e44fb85dc9e8
 ---> d4c285f6e797
Step 4/9 : COPY index.html ${DOC_ROOT}
 ---> af1474799055
Step 5/9 : COPY yum.repos.d /etc/yum.repos.d/
 ---> 36faa70c6492
Step 6/9 : WORKDIR /usr/local/
 ---> Running in e7fb97f47a8b
Removing intermediate container e7fb97f47a8b
 ---> 5bc3ce07dece
Step 7/9 : ADD ${WEB_SERVER_PACKAGE}.tar.gz ./src/
 ---> 0261977a8806
Step 8/9 : VOLUME /data/mysql
 ---> Running in 2f6eb9f81e85
Removing intermediate container 2f6eb9f81e85
 ---> 9da361733637
Step 9/9 : EXPOSE 8080:80/tcp
 ---> Running in d4ef36535a75
Removing intermediate container d4ef36535a75
 ---> 1e9d1aa2a909
Successfully built 1e9d1aa2a909
Successfully tagged busyboxhttpd:v0.7

# 基于此新建镜像运行容器,进行验证
[root@localhost test]# docker run --name web1 -P --rm -it busyboxhttpd:v0.7 ls /usr/local/src /data/web/html
/data/web/html:
index.html

/usr/local/src:
nginx-1.15.8

# 也可以使用printenv查看变量验证
[root@localhost test]# docker run --name web1 --rm -it busyboxhttpd:v0.7 printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=5cfec5e2996a
TERM=xterm
DOC_ROOT=/data/web/html/
WEB_SERVER_PACKAGE=nginx-1.15.8
HOME=/root

# 在启动容器时,可以使用docker run -e 设置修改变量
[root@localhost test]# docker run --name web1 -e WEB_SERVER_PACKAGE=nginx-1.15.7 --rm -it busyboxhttpd:v0.7 printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=109927902a7b
TERM=xterm
WEB_SERVER_PACKAGE=nginx-1.15.7
DOC_ROOT=/data/web/html/
HOME=/root

7.10. RUN

介绍:

  • 用于指定docker build过程中运行的程序,可以是任何命令

格式:

RUN <command> 或
RUN [“<executable>”,”<param1>”,”<param2>”]

注:
	第一种格式中,<command>通常是一个shell命令,且以”/bin/sh -c“来运行,这意味着此进程在容器中的PID不为1,不能接收Unix信号。因此,当使用docker stop <container>命令停止容器时,此进程接收不到SIGTERM信号;
	第二种语法格式中的参数是一个JSON格式数组,其中<executable>为要运行的命令,后面的<paramN>为传递给命令的选项或参数;然而,此种格式指定的命令不会以”/bin/sh -c”来发起,因此常见的shell操作如变量替换以及通配符替换将不会进行;

示例:

# 编写dockerfile文件
[root@localhost test]# vim dockerfile
FROM busybox:latest
MAINTAINER "Along <along@along.com>"
ENV DOC_ROOT=/data/web/html/ 
  WEB_SERVER_PACKAGE="nginx-1.15.8.tar.gz"

COPY index.html ${DOC_ROOT}
COPY yum.repos.d /etc/yum.repos.d/

WORKDIR /usr/local/
ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} /usr/local/src/
VOLUME /data/mysql
EXPOSE 8080:80/tcp

RUN cd ./src && 
  tar -xf ${WEB_SERVER_PACKAGE}

# 使用build制作镜像
[root@localhost test]# docker build -t busyboxhttpd:v0.8 ./
Sending build context to Docker daemon  1.042MB
Step 1/10 : FROM busybox:latest
 ---> 6d5fcfe5ff17
Step 2/10 : MAINTAINER "Along <along@along.com>"
 ---> Using cache
 ---> c914e57b9397
Step 3/10 : ENV DOC_ROOT=/data/web/html/   WEB_SERVER_PACKAGE="nginx-1.15.8.tar.gz"
 ---> Running in ee9a1a01d164
Removing intermediate container ee9a1a01d164
 ---> cbdd592e4f00
Step 4/10 : COPY index.html ${DOC_ROOT}
 ---> 27c6c80679c9
Step 5/10 : COPY yum.repos.d /etc/yum.repos.d/
 ---> ceeb8538f7c2
Step 6/10 : WORKDIR /usr/local/
 ---> Running in d16b6cf4b5da
Removing intermediate container d16b6cf4b5da
 ---> ccdcebfa33c2
Step 7/10 : ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} /usr/local/src/
Downloading  1.028MB/1.028MB
 ---> a3a2583d2f64
Step 8/10 : VOLUME /data/mysql
 ---> Running in 4da58911f43e
Removing intermediate container 4da58911f43e
 ---> 68298d142c14
Step 9/10 : EXPOSE 8080:80/tcp
 ---> Running in 58ff6aff7f63
Removing intermediate container 58ff6aff7f63
 ---> 23b82bed52b1
Step 10/10 : RUN cd ./src &&   tar -xf ${WEB_SERVER_PACKAGE}
 ---> Running in 4ad6a4d4e453
Removing intermediate container 4ad6a4d4e453
 ---> e919f81f65ce
Successfully built e919f81f65ce
Successfully tagged busyboxhttpd:v0.8

# 基于此新建镜像运行容器,进行验证
[root@localhost test]# docker run --name web1 -P --rm -it busyboxhttpd:v0.8 ls /usr/local/src/
nginx-1.15.8         nginx-1.15.8.tar.gz

7.11. CMD

介绍:

  • 类似RUN指令,CMD指令也可以用于运行任何命令或应用程序;
  • RUN指令运行于映像文件构建过程中,而CMD指令运行于基于Dockerfile构建出新的映像文件启动一个容器时
  • CMD指令的首要目的在于为启动的容器指定默认要运行的程序,且其运行结束后,容器也将终止;不过,CMD指定的命令可以被docker run的命令行选项所覆盖

格式:

CMD <command> 或
CMD [“<executable>”,”<param1>”,”<param2>”] 或
CMD [“<param1>”,”<param2>”]

注:
	前两种语法格式的意义同RUN
	第三种则用于为ENTRYPOINT指令提供默认参数
	Json数组中,要使用双引号,单引号会出错

示例:

# 编写dockerfile文件
[root@localhost test]# vim dockerfile
MAINTAINER "Along <along@along.com>"
ENV DOC_ROOT=/data/web/html/
RUN mkdir -p ${DOC_ROOT} && 
  echo "<h1>Busybox httpd server</h1>" > ${DOC_ROOT}/index.html
CMD /bin/httpd -f -h ${DOC_ROOT}

# 使用build制作镜像
[root@localhost test]# docker build -t busyboxhttpd:v1.1 ./
Sending build context to Docker daemon  1.043MB
Step 1/5 : FROM busybox:latest
 ---> 6d5fcfe5ff17
Step 2/5 : MAINTAINER "Along <along@along.com>"
 ---> Using cache
 ---> c914e57b9397
Step 3/5 : ENV DOC_ROOT=/data/web/html/
 ---> Running in 5034f373a0da
Removing intermediate container 5034f373a0da
 ---> 61385ce085f2
Step 4/5 : RUN mkdir -p ${DOC_ROOT} &&   echo "<h1>Busybox httpd server</h1>" > ${DOC_ROOT}/index.html
 ---> Running in 81c7537cae91
Removing intermediate container 81c7537cae91
 ---> 3a43f0e2e856
Step 5/5 : CMD /bin/httpd -f -h ${DOC_ROOT}
 ---> Running in c7906ee135fa
Removing intermediate container c7906ee135fa
 ---> 55a9c0733461
Successfully built 55a9c0733461
Successfully tagged busyboxhttpd:v1.1

# 基于此新建镜像运行容器,进行验证
[root@localhost test]# docker run --name web2 --rm -d busyboxhttpd:v1.1
480644d174f57eb4c612eec3cd2160a82f9bb0930687299b2482829d282ccfc7
[root@localhost test]# docker inspect -f {{.NetworkSettings.IPAddress}} web2
172.17.0.2
[root@localhost test]# curl 172.17.0.2
<h1>Busybox httpd server</h1>
[root@localhost test]# docker exec -it web2 /bin/sh
/ # ps
PID   USER     TIME  COMMAND
    1 root      0:00 /bin/httpd -f -h /data/web/html/
    7 root      0:00 /bin/sh
   12 root      0:00 ps
/ # printenv
HOSTNAME=480644d174f5
DOC_ROOT=/data/web/html/

7.12. ENTRYPOINT

介绍:

  • 类似于CMD指令的功能,用于为容器指定默认运行程序,从而使得容器像是一个单独的可执行程序;
  • 与CMD不同的是,有ENTRYPOINT启动的程序不会被docker run命令指定的参数覆盖,而且,这些命令参数会被当做参数传递给ENTRYPOTINT指定的程序
  • 不过,docker run命令的—entrypoint选项的参数可覆盖ENTRYPOINT指令指定的程序;

格式:

ENTRYPOINT <command>
ENTRYPOINT [“<executable>”,”<param1>”,”<param2>”]

注:
	Docker run命令传入的命令参数会覆盖CMD指令的内容并且附加到ENTRYPOINT命令最后作为其参数使用;
	Dockerfile文件中也可以存在多个ENTRYPOINT指令,但仅有最后一个会生效

示例:

# 编写dockerfile文件
[root@localhost test]# vim dockerfile
FROM busybox:latest
MAINTAINER "Along <along@along.com>"
ENV DOC_ROOT=/data/web/html/
RUN mkdir -p ${DOC_ROOT} && 
  echo "<h1>Busybox httpd server</h1>" > ${DOC_ROOT}/index.html
# CMD /bin/httpd -f -h ${DOC_ROOT}
ENTRYPOINT /bin/httpd -f -h ${DOC_ROOT}

# 使用build制作镜像
[root@localhost test]# docker build -t busyboxhttpd:v1.2 ./
Sending build context to Docker daemon  1.043MB
Step 1/5 : FROM busybox:latest
 ---> 6d5fcfe5ff17
Step 2/5 : MAINTAINER "Along <along@along.com>"
 ---> Using cache
 ---> c914e57b9397
Step 3/5 : ENV DOC_ROOT=/data/web/html/
 ---> Using cache
 ---> 61385ce085f2
Step 4/5 : RUN mkdir -p ${DOC_ROOT} &&   echo "<h1>Busybox httpd server</h1>" > ${DOC_ROOT}/index.html
 ---> Using cache
 ---> 3a43f0e2e856
Step 5/5 : ENTRYPOINT /bin/httpd -f -h ${DOC_ROOT}
 ---> Running in af80bed25a1e
Removing intermediate container af80bed25a1e
 ---> f44109a73c3f
Successfully built f44109a73c3f
Successfully tagged busyboxhttpd:v1.2

# 基于此新建镜像运行容器,进行验证
[root@localhost test]# docker run --name web2 --rm busyboxhttpd:v1.2 ls /
# 发现不会执行ls / 这个命令;任然执行的ENTRYPOINT中设置的命令;
[root@localhost ~]# curl 172.17.0.2
<h1>Busybox httpd server</h1>

7.13. HEALTHCHECK

介绍:

  • HEALTHCHECK指令告诉docker如何测试容器以检查它是否仍在工作;
  • 即使服务器进程仍在运行,这也可以检测出陷入无限循环且无法处理新连接的web服务器等情况;

格式:

HEALTHCHECK [OPTIONS] CMD command (通过在容器内运行命令来检查容器运行状况)
HEALTHCHECK NONE (禁用从基础映像继承的任何运行状况检查)

OPTIONS选项:
	--interval=DURATION (default:30s) 每隔多长时间探测一次,默认30秒;
	--tiemout=DURATION (default:30s) 服务响应超时时间,默认30秒;
	--start-period=DURATION (default:0s) 服务启动多久开始探测,默认0秒
	--retries=N (default:3 ) 认为检测失败几次为宕机,默认3次

返回值:
0	容器成功是健康的,随时可以使用
1	不健康的容器无法正常工作
2	保留不适用此退出代码

示例:

# 编写dockerfile文件
[root@localhost test]# vim dockerfile
FROM busybox:latest
MAINTAINER "Along <along@along.com>"
ENV DOC_ROOT=/data/web/html/
RUN mkdir -p ${DOC_ROOT} && 
  echo "<h1>Busybox httpd server</h1>" > ${DOC_ROOT}/index.html
CMD /bin/httpd -f -h ${DOC_ROOT}
# 检测web2容器的10080端口(10080端口并没有打开,所有检测会失败)
HEALTHCHECK --start-period=3s CMD wget -O --q http://${IP:-0.0.0.0}:10080/

# 使用build制作镜像
[root@localhost test]# docker build -t busyboxhttpd:v1.3 ./
Sending build context to Docker daemon  1.043MB
Step 1/6 : FROM busybox:latest
 ---> 6d5fcfe5ff17
Step 2/6 : MAINTAINER "Along <along@along.com>"
 ---> Using cache
 ---> c914e57b9397
Step 3/6 : ENV DOC_ROOT=/data/web/html/
 ---> Using cache
 ---> 61385ce085f2
Step 4/6 : RUN mkdir -p ${DOC_ROOT} &&   echo "<h1>Busybox httpd server</h1>" > ${DOC_ROOT}/index.html
 ---> Using cache
 ---> 3a43f0e2e856
Step 5/6 : CMD /bin/httpd -f -h ${DOC_ROOT}
 ---> Using cache
 ---> 55a9c0733461
Step 6/6 : HEALTHCHECK --start-period=3s CMD wget -O --q http://${IP:-0.0.0.0}:10080/
 ---> Running in 0279ae2293a3
Removing intermediate container 0279ae2293a3
 ---> 99f13e1b91f2
Successfully built 99f13e1b91f2
Successfully tagged busyboxhttpd:v1.3

# 基于此新建镜像运行容器,进行验证
[root@localhost test]# docker run --name web2 --rm -d busyboxhttpd:v1.3
155134e06dbab478d571dd42dd2ccdd1641a7458339132302a85dc26fdff851b
# 检测不到10080端口,容器会变为unhealthy不健康状态
[root@localhost test]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
155134e06dba        busyboxhttpd:v1.3   "/bin/sh -c '/bin/ht…"   4 minutes ago       Up 4 minutes (unhealthy)

7.14. ONBUILD

介绍:

  • 用于在dockerfile中定义一个触发器
  • Dockerfile用于build映像文件,此映像文件亦可作为base image被另一个dockerfile用作FROM指令的参数,并以之构建新的映像文件;
  • 在后面的而这个docerfile中的FROM指令在build过程中被执行时,将会“触发”创建其base image的dockerfile文件中的ONBUILD指令定义的触发器

格式:

ONBUILD <Instruction>
	尽管任何指令都可以注册成为触发器指令,但ONBUILD不能自我嵌套,且不会触发FROM和MAINTAINER指令;
	使用包含ONBUILD指令的Dockerfile构建的镜像应该使用特殊的标签,例如:ruby:2.0-onbuild;
	在ONBUILD指令中使用ADD或COPY指令应该格外小心,因为新构建过程的上下文在缺少指定的源文件时会失败;

示例:

# 编写dockerfile1文件
[root@localhost test]# cat dockerfile1
FROM busybox:latest
MAINTAINER "Along <along@along.com>"
ENV DOC_ROOT=/data/web/html/
RUN mkdir -p ${DOC_ROOT} && 
  echo "<h1>Busybox httpd server</h1>" > ${DOC_ROOT}/index.html
ONBUILD RUN echo "<h1>Busybox httpd server</h1>" > ${DOC_ROOT}/index.html
# 编写dockerfile2文件
[root@localhost test]# cat dockerfile2
FROM busyboxhttpd:v2.1

# 使用build制作镜像
# 基于dockerfile1文件新建镜像,并启容器,进行验证
[root@localhost test]# docker build -f dockerfile1 -t busyboxhttpd:v2.1 ./
Sending build context to Docker daemon  1.046MB
Step 1/5 : FROM busybox:latest
 ---> 6d5fcfe5ff17
Step 2/5 : MAINTAINER "Along <along@along.com>"
 ---> Using cache
 ---> c914e57b9397
Step 3/5 : ENV DOC_ROOT=/data/web/html/
 ---> Using cache
 ---> 61385ce085f2
Step 4/5 : RUN mkdir -p ${DOC_ROOT} &&   echo "<h1>Busybox httpd server</h1>" > ${DOC_ROOT}/index.html
 ---> Using cache
 ---> 3a43f0e2e856
Step 5/5 : ONBUILD RUN echo "<h1>Busybox httpd server</h1>" > ${DOC_ROOT}/index.html
 ---> Running in f01cd6bcb60e
Removing intermediate container f01cd6bcb60e
 ---> 592af9d57cd1
Successfully built 592af9d57cd1
Successfully tagged busyboxhttpd:v2.1

[root@localhost test]# docker run --name web2 --rm busyboxhttpd:v2.1 cat /data/web/html/index.html
<h1>Busybox httpd server</h1>


# 基于dockerfile2文件新建镜像,并启容器,进行验证
[root@localhost test]# docker build -f dockerfile2 -t busyboxhttpd:v2.2 ./
Sending build context to Docker daemon  1.046MB
Step 1/1 : FROM busyboxhttpd:v2.1
# Executing 1 build trigger
 ---> Running in 1bb566c0eebb
Removing intermediate container 1bb566c0eebb
 ---> d9d34237f29f
Successfully built d9d34237f29f
Successfully tagged busyboxhttpd:v2.2

[root@localhost test]# docker run --name web2 --rm busyboxhttpd:v2.2 cat /data/web/html/index.html
<h1>Busybox httpd server</h1>

# 证明ONBUILD指令,只在第二个dockerfile文件中生效
原文地址:https://www.cnblogs.com/HsLM/p/12463960.html