• 09、dockerfile详解


    1、dockerfile介绍

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

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

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

    1.1、dockerfile指令

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

    1.2、dockerfile的使用

    1)dockerfile编写的基本结构

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

    2)一台主机可以有多个dockerfile

    要使用多个dockerfile创建镜像,可以在不同目录编写dockerfile,然后再dockerfile所在的目录下构建新的镜像;

    注意:dockerfile中所包含的需要的内容;如copy的文件、目录等,都需要再dockerfile同级目录下存在

    3)docker build基于dockerfile制作镜像的命令

    格式:

    docker build [OPTIONS] PATH | URL

    选项:

      -t:打标签

      -c,-cpu-shares init:CPU份额(相对权重)

      -m,-memroy bytes:内存限制

      --build-arg:设置构建是变量,就是构建的时候修改ARG指令的参数

    2、FROM指令

    2.1、介绍

    FROM指令必须是dockerfile中非注释行的第一个指令,即一个dockerfile从FROM语句开始;

    FROM指令用于为镜像文件构建过程指定基础镜像,后续的指令运行于此基础镜像所提供的运行环境

    实践中,基础镜像可以是任何可用镜像文件,默认情况下,docker build 会在docker主机上查找指定的镜像文件,在其不存在时,则会自动从docker的公共库pull镜像下来。如果找不到指定的镜像文件,docker build会返回一个错误信息

    FROM可以在一个dockerfile中出现多次,如果有需求在一个dockerfile中创建多个镜像

    如果FROM语句没有指定镜像标签,则默认使用latest标签

    2.2、格式

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

    注:

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

    2.3、示例

    # Description: test image
    FROM busybox:latest

    3、MAINTAINER

    用于让dockerfile制作者提供本人的详细信息

    dockerfile并不限制MAINTAINET指令可在出现的位置,但推荐将其放置于FROM指令之后

    3.1、格式

    MAINTAINER <authtor's detail>

    3.2、示例

    # Description: test image
    FROM busybox:latest
    MAINTAINER "hackerlin <hackerlin@hackerlin.com>"

    4、copy

    4.1、介绍

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

    4.2、格式

    COPY <src>...<dest>或者
    COPU["<src>"..."<dest>"]

    注:

    <src>:要赋值的源文件或目录,支持使用通配符;

    <dest>:目标路径,即正则创建的images的文件系统路径;建议<dest>使用绝对路径,否则,COPY指定以workdir为其路径

    在路径中有空白字符时,通常使用第2种格式

    4.3、文件赋值准则

    <src>必须是build上下文中的路径,不能是其父目录中的文件;

    如果<src>是目录,则其内部文件或子目录会被递归复制,但<src>目录自身不会被复制;

    如果指定了多个<src>,或在<src>中使用了通配符,则<dest>必须是一个目录,且必须以/结尾

    如果<dest>事先不存在,他将会被自动创建,这包括父目录路径

    4.4、示例

    1)copy文件

    编写dockerfile文件

    #Description:test image
    FROM docker.io/busybox:latest
    MAINTAINER "linhacker<linhacker@linhacker.com>"
    COPY index.html /data/web/html/         #要确保dockerfile同级目录下有index.html文件

    在dockerfile同级目录下准备好index.html文件

    [root@hackerlin dockerfile]#vim index.html
    <h1>Busybox httpd server</h1>

    使用build制作镜像

    [root@hackerlin dockerfile]# docker build -t busyboxhttpd:v0.1 .
    Sending build context to Docker daemon 3.072 kB
    Step 1/3 : FROM docker.io/busybox:latest
     ---> 83aa35aa1c79
    Step 2/3 : MAINTAINER "linhacker<linhacker@linhacker.com>"
     ---> Using cache
     ---> b61c3f61df4b
    Step 3/3 : COPY index.html /data/web/html/
     ---> 36449fb8e2d1
    Removing intermediate container 71055330267c
    Successfully built 36449fb8e2d1

    基于此新镜像运行容器,进行验证

    [root@hackerlin dockerfile]# docker run --name web1 --rm busyboxhttpd:v0.1 cat /data/web/html/index.html
    WARNING: IPv4 forwarding is disabled. Networking will not work.
    <h1>Busybox httpd server</h1>

    注:--rm 在容器关闭时,直接删除容器,方便实验

    2)copy目录

    编写Dockerfile文件

    #Description:test image
    FROM docker.io/busybox:latest
    MAINTAINER "linhacker<linhacker@linhacker.com>"
    COPY index.html /data/web/html/
    COPY yum.repos.d /etc/yum.repos.d/

    注:如果是复制目录,则其内部文件或子目录会被递归复制,但<src>目录自身不会被复制;需要把复制目录名字也写在容器中药复制的路径下!

    在dockerfile同级目录下准备好与,yum.repos.d目录

    [root@hackerlin dockerfile]# cp -r /etc/yum.repos.d/ ./
    [root@hackerlin dockerfile]# ls yum.repos.d/
    CentOS-Base.repo  CentOS-CR.repo  CentOS-Debuginfo.repo  CentOS-fasttrack.repo  CentOS-Media.repo  CentOS-Sources.repo  CentOS-Vault.repo

    使用build 来制作镜像

    [root@hackerlin dockerfile]# docker build -t busyboxhttpd:v0.2 ./
    Sending build context to Docker daemon  21.5 kB
    Step 1/4 : FROM docker.io/busybox:latest
     ---> 83aa35aa1c79
    Step 2/4 : MAINTAINER "linhacker<linhacker@linhacker.com>"
     ---> Using cache
     ---> b61c3f61df4b
    Step 3/4 : COPY index.html /data/web/html/
     ---> Using cache
     ---> 36449fb8e2d1
    Step 4/4 : COPY yum.repos.d /etc/yum.repos.d/
     ---> fb95681e08ee
    Removing intermediate container 6ac114ea24ed
    Successfully built fb95681e08ee

    基于此新建镜像运行容器,进行验证

    [root@hackerlin dockerfile]# docker run --name web1 --rm busyboxhttpd:v0.2 ls /etc/yum.repos.d/
    WARNING: IPv4 forwarding is disabled. Networking will not work.
    CentOS-Base.repo
    CentOS-CR.repo
    CentOS-Debuginfo.repo
    CentOS-Media.repo
    CentOS-Sources.repo
    CentOS-Vault.repo
    CentOS-fasttrack.repo

    5、ADD

    5.1、介绍

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

    5.2、格式

    ADD <src>..<dest>或
    
    ADD["<src>".."<dest>"]

    5.3、操作准则

    同copy命令

    如果<src>为URL且<dest>不以“/”结尾。则<src>指定的文件将被下载并直接被创建为<dest>;如果<dest>以"/"结尾,则文件名URL指定的文件将直接下载并保存为<dest>/<filename>

    如果<src>是一个本地系统上的压缩格式的tar文件,它将被展开为一个目录,其行为类似于“tar -x”命令;然而,通过URL获取到的tar文件将不会自动展开

    如果<src>有多个,或其间接或直接使用了通配符,则<dest>必须是一个以/结尾的目录路径;如果<dest>不以/结尾,则其被视作一个普通文件,<src>内容将被直接写入到<dest>

    5.4、示例

    1)copy网上路径(URL)的tar包

    编写dockerfile文件

    #Description:test image
    FROM docker.io/busybox:latest
    MAINTAINER "linhacker<linhacker@linhacker.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的<src>是网上的nginx下载路径

    使用build制作镜像

    [root@hackerlin dockerfile]# docker build -t busyboxhttpd:v0.3 ./
    Sending build context to Docker daemon  21.5 kB
    Step 1/5 : FROM docker.io/busybox:latest
     ---> 83aa35aa1c79
    Step 2/5 : MAINTAINER "linhacker<linhacker@linhacker.com>"
     ---> Using cache
     ---> b61c3f61df4b
    Step 3/5 : COPY index.html /data/web/html/
     ---> Using cache
     ---> 36449fb8e2d1
    Step 4/5 : COPY yum.repos.d /etc/yum.repos.d/
     ---> Using cache
     ---> fb95681e08ee
    Step 5/5 : ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
    Downloading [==================================================>] 1.028 MB/1.028 MB
     ---> 24a8c1f470ca
    Removing intermediate container d27fe5411f6c
    Successfully built 24a8c1f470ca

    基于此新镜像运行容器,并验证

    [root@hackerlin dockerfile]# docker run --name web1 --rm busyboxhttpd:v0.3 ls /usr/local/src
    WARNING: IPv4 forwarding is disabled. Networking will not work.
    nginx-1.15.8.tar.gz

    2)copy本地的路径的tar包

    编写dockerfile文件

    #Description:test image
    FROM docker.io/busybox:latest
    MAINTAINER "linhacker<linhacker@linhacker.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同级目录下准备好nginx-1.15.8.tar.gz文件

    [root@hackerlin dockerfile]# wget http://nginx.org/download/nginx-1.15.8.tar.gz

    使用build制作镜像

    [root@hackerlin dockerfile]# docker build -t busyboxhttpd:v0.4 ./
    Sending build context to Docker daemon  1.05 MB
    Step 1/5 : FROM docker.io/busybox:latest
     ---> 83aa35aa1c79
    Step 2/5 : MAINTAINER "linhacker<linhacker@linhacker.com>"
     ---> Using cache
     ---> b61c3f61df4b
    Step 3/5 : COPY index.html /data/web/html/
     ---> Using cache
     ---> 36449fb8e2d1
    Step 4/5 : COPY yum.repos.d /etc/yum.repos.d/
     ---> Using cache
     ---> fb95681e08ee
    Step 5/5 : ADD nginx-1.15.8.tar.gz /usr/local/src/
     ---> 33e07f56f3cd
    Removing intermediate container f77044ee08b0
    Successfully built 33e07f56f3cd

    使用此新镜像运行容器,并验证

    [root@hackerlin dockerfile]# docker run --name web1 --rm busyboxhttpd:v0.4 ls /usr/local/src
    WARNING: IPv4 forwarding is disabled. Networking will not work.
    nginx-1.15.8

    6、WORKDIR

    6.1、介绍

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

    6.2、格式

    WORKDIR    <dirpath>

    在dockerfile文件中,WORKDIR指令可以出现多次,其路径也可以为相对路径,不过,其实相对此前一个WORKDIR指令指定的路径

    宁外,WORKDIR也可以调用有ENV指定定义的变量

    6.3、示例

    #Description:test image
    FROM docker.io/busybox:latest
    MAINTAINER "linhacker<linhacker@linhacker.com>"
    COPY index.html /data/web/html/
    COPY yum.repos.d /etc/yum.repos.d/
    WORKDIR /usr/local
    ADD nginx-1.15.8.tar.gz ./src/

     7、VOLUME

    7.1、介绍

    volume用于在images中创建一个挂载点目录,以挂载daocker host上的卷或其它容器上的卷

    7.2、语法

    VOLUME <mountpoint>或
    VOLUME["<mountpoint>"]

    注:如果挂载点目录路径下此前文件存在,docker run 命令会在卷挂载完成之后将此前的所有文件赋值到新挂载的卷中

    7.3、示例

    编写dockerfile

    #Description:test image
    FROM docker.io/busybox:latest
    MAINTAINER "linhacker<linhacker@linhacker.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/
    
    VOLUME /data/mysql

    使用build制作镜像

    [root@hackerlin dockerfile]# docker build -t busyboxhttpd:v0.5 ./
    Sending build context to Docker daemon 1.063 MB
    Step 1/6 : FROM docker.io/busybox:latest
     ---> 83aa35aa1c79
    Step 2/6 : MAINTAINER "linhacker<linhacker@linhacker.com>"
     ---> Using cache
     ---> b61c3f61df4b
    Step 3/6 : COPY index.html /data/web/html/
     ---> Using cache
     ---> 36449fb8e2d1
    Step 4/6 : COPY yum.repos.d /etc/yum.repos.d/
     ---> Using cache
     ---> fb95681e08ee
    Step 5/6 : ADD nginx-1.15.8.tar.gz /usr/local/src/
     ---> Using cache
     ---> 33e07f56f3cd
    Step 6/6 : VOLUME /data/mysql
     ---> [Warning] IPv4 forwarding is disabled. Networking will not work.
     ---> Running in ccf86c12238c
     ---> 4526454a743f
    Removing intermediate container ccf86c12238c
    Successfully built 4526454a743f

    基于此镜像运行容器,进行验证

    [root@hackerlin dockerfile]# docker run --name web1 --rm -itd busyboxhttpd:v0.5 /bin/sh
    [root@hackerlin dockerfile]# docker inspect -f {{.Mounts}} web1
    [{volume 4dea5e92f25a343e7b9022c14156a74faecb4ee6ef3d900706b81fac4451b342 /var/lib/docker/volumes/4dea5e92f25a343e7b9022c14156a74faecb4ee6ef3d900706b81fac4451b342/_data /data/mysql local  true }]

    8、EXPOSE

    8.1、介绍

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

    8.2、语法

    EXPOSE <port> [/<protocol>] <port>[/<protocol>]....

    注:

      <protocol>用于指定传输层协议,可为tcp或udp二者之一,默认为TCP协议

      EXPOSE 指令可一次指定多个端口,例如EXPOSE 11211/TCP 11211/udp

    8.3、示例

    编写dockerfile文件

    #Description:test image
    FROM docker.io/busybox:latest
    MAINTAINER "linhacker<linhacker@linhacker.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/
    
    VOLUME  /data/mysql
    
    EXPOSE 80/tcp

    使用build制作镜像

    [root@hackerlin dockerfile]# docker build -t busyboxhttpd:v0.6 ./
    Sending build context to Docker daemon 1.063 MB
    Step 1/7 : FROM docker.io/busybox:latest
     ---> 83aa35aa1c79
    Step 2/7 : MAINTAINER "linhacker<linhacker@linhacker.com>"
     ---> Using cache
     ---> b61c3f61df4b
    Step 3/7 : COPY index.html /data/web/html/
     ---> Using cache
     ---> 36449fb8e2d1
    Step 4/7 : COPY yum.repos.d /etc/yum.repos.d/
     ---> Using cache
     ---> fb95681e08ee
    Step 5/7 : ADD nginx-1.15.8.tar.gz /usr/local/src/
     ---> Using cache
     ---> 33e07f56f3cd
    Step 6/7 : VOLUME /data/mysql
     ---> Using cache
     ---> 4526454a743f
    Step 7/7 : EXPOSE 80/tcp
     ---> [Warning] IPv4 forwarding is disabled. Networking will not work.
     ---> Running in 268532df13db
     ---> 4bca327a0473
    Removing intermediate container 268532df13db
    Successfully built 4bca327a0473

    基于此新建镜像运行容器,进行验证

    [root@hackerlin ~]# docker run --name web1 -P --rm -it busyboxhttpd:v0.6 /bin/httpd -f -h /data/web/html
    --- 另打开一个终端,验证httpd 服务的80端口
    [root@hackerlin ~]# docker inspect -f {{.NetworkSettings.IPAddress}} web1  #查询容器的IP
    172.17.0.2
    [root@hackerlin ~]# curl 172.17.0.2:80
    <h1>Busybox httpd server</h1>
    --- 在宿主机通过暴露的端口访问httpd 服务
    [root@hackerlin ~]# docker port web1
    80/tcp -> 0.0.0.0:32768
    [root@along ~]# curl 127.0.0.1:32768
    <h1>Busybox httpd server</h1>

    注:就算dockerfile 中有EXPOSE 指令暴露端口,但是不是真正的暴露;需要在启动容器时,使用-P 选项真正的暴露端口。

     

    9、ENV

    9.1、介绍

    用于为镜像定义所需的环境变量,并可被dockerfile文件中位于其后的其他指令(如ENV、ADD、COPY等)所调用

    调用格式为$variable_name或${variable_name}

    9.2、格式

     

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

     

    注:

      第一种格式中,<key>之后的所有内容均会被视作其<value>的组成部分,因此,一次只能设置一个变量

      第二种格式可用一次设置多个变量,每个变量为一个<key>=<value>的键值对,如果<value>中包含空格,可以以反斜线()进行转义,也可通过对<value>加引号进行标识;另外,反斜线也可用于读行

      定义多个变量是,建议使用第二种方式,以便在同一层中完成所有功能

    9.3、示例

    编写dockerfile

    #Description:test image
    FROM docker.io/busybox:latest
    MAINTAINER "linhacker<linhacker@linhacker.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制作镜像

     

     

    Sending build context to Docker daemon 1.056 MB
    Step 1/9 : FROM busybox:latest
     ---> 758ec7f3a1ee
    Step 2/9 : MAINTAINER "Along <along@along.com>"
     ---> Using cache
     ---> 5d2cda1631d7
    Step 3/9 : ENV DOC_ROOT /data/web/html/ WEB_SERVER_PACKAGE "nginx-1.15.8"
     ---> Running in b268b672236a
     ---> 295bf19a8395
    Removing intermediate container b268b672236a
    Step 4/9 : COPY index.html ${DOC_ROOT}
     ---> c29d3c8f6e0d
    Removing intermediate container 33238b2ba6bf
    Step 5/9 : COPY yum.repos.d /etc/yum.repos.d/
     ---> 3a2a2f571b07
    Removing intermediate container bc49392442bc
    Step 6/9 : WORKDIR /usr/local/
     ---> a695a7f0265f
    Removing intermediate container aa12de3e47db
    Step 7/9 : ADD ${WEB_SERVER_PACKAGE}.tar.gz ./src/
     ---> aca798ede129
    Removing intermediate container 4cf601151915
    Step 8/9 : VOLUME /data/mysql
     ---> Running in 497595d537a7
     ---> 8f52b722011c
    Removing intermediate container 497595d537a7
    Step 9/9 : EXPOSE 8080:80/tcp
     ---> Running in 1dac584db62d
     ---> b10c56601efb
    Removing intermediate container 1dac584db62d
    Successfully built b10c56601efb

     

    基于此新建镜像运行容器,进行验证

    [root@hackerlin ~]# 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@hackerlin ~]# docker run --name web1 --rm -it busyboxhttpd:v0.7
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    DOC_ROOT=/data/web/html/
    WEB_SERVER_PACKAGE=nginx-1.15.8
    --- 在启动容器时,使用docker run -e 设置修改变量
    [root@hackerlin ~]# 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
    WEB_SERVER_PACKAGE=nginx-1.15.7
    DOC_ROOT=/data/web/html/

    10 RUN

    10.1 介绍

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

    10.2 语法

    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操作如变量替换以及通配符(?,*)替换将不会进行;不过,如果要运行的命令依赖于此shell特性的话,可以将其替换为类似下面的格式。
    • RUN ["/bin/bash", "-c", "<executable>", "<param1>"]

     

    10.3 示例

    1)编写dockerfile文件:使用RUN 执行解压命令

     

    # Description: test image
    
    FROM busybox:latest
    
    MAINTAINER "hackerlin <hackerlin@hackerlin.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} ./src/
    
    #ADD ${WEB_SERVER_PACKAGE} ./src/
    
     
    
    VOLUME /data/mysql
    
     
    
    EXPOSE 8080:80/tcp
    
     
    
    RUN cd ./src && 
    
        tar -xf ${WEB_SERVER_PACKAGE}

     

    2)使用build 制作镜像  

     

    [root@hackerlin img1]# docker build -t busyboxhttpd:v0.8 ./
    
    Sending build context to Docker daemon 1.056 MB
    
    Step 1/10 : FROM busybox:latest
    
     ---> 758ec7f3a1ee
    
    Step 2/10 : MAINTAINER "hackerlin <hackerlin@hackerlin.com>"
    
     ---> Using cache
    
     ---> 5d2cda1631d7
    
    Step 3/10 : ENV DOC_ROOT /data/web/html/ WEB_SERVER_PACKAGE "nginx-1.15.8.tar.gz"
    
     ---> Running in ff660867b971
    
     ---> fef5c3f0295a
    
    Removing intermediate container ff660867b971
    
    Step 4/10 : COPY index.html ${DOC_ROOT}
    
     ---> 825ae434ab56
    
    Removing intermediate container 0e22f2a6b9c2
    
    Step 5/10 : COPY yum.repos.d /etc/yum.repos.d/
    
     ---> 33b5aa2e5b9b
    
    Removing intermediate container 51722b8fcee3
    
    Step 6/10 : WORKDIR /usr/local/
    
     ---> 31912ce0b9b1
    
    Removing intermediate container ecfe195635a2
    
    Step 7/10 : ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} ./src/
    
    Downloading 1.028 MB/1.028 MB
    
     ---> 5d46a9337dfe
    
    Removing intermediate container d38191e8b1ff
    
    Step 8/10 : VOLUME /data/mysql
    
     ---> Running in 5c18e8b76af2
    
     ---> 2d1ee498340b
    
    Removing intermediate container 5c18e8b76af2
    
    Step 9/10 : EXPOSE 8080:80/tcp
    
     ---> Running in 8c32c08cdb78
    
     ---> 17700871c846
    
    Removing intermediate container 8c32c08cdb78
    
    Step 10/10 : RUN cd ./src &&     tar -xf ${WEB_SERVER_PACKAGE}
    
     ---> Running in 555b43cc59db
    
     ---> 21973fc25ad4
    
    Removing intermediate container 555b43cc59db
    
    Successfully built 21973fc25ad4

     

    3)基于此新建镜像运行容器,进行验证:已经执行了解压命令  

     

    [root@hackerlin ~]# docker run --name web1 -P --rm -it busyboxhttpd:v0.7 ls /usr/local/src
    
    nginx-1.15.8

     

    11 CMD  

    11.1 介绍

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

    11.2 语法

     

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

     

    前两种语法格式的意义同RUN注:

    • 第三种则用于为ENTRYPOINT指令提供默认参数
    • json数组中,要使用双引号,单引号会出错

     

    11.3 示例

    1)编写dockerfile文件

     

    FROM busybox
    
    LABEL maintainer="hackerlin <hackerlin@hackerlin.com>" app="httpd"
    
     
    
    ENV WEB_DOC_ROOT="/data/web/html"
    
     
    
    RUN mkdir -p ${WEB_DOC_ROOT} && 
    
        echo "<h1>Busybox httpd server</h1>" > ${WEB_DOC_ROOT}/index.html
    
     
    
    CMD /bin/httpd -f -h ${WEB_DOC_ROOT}

     

    2)使用build 制作镜像  

     

    [root@hackerlin image2]# docker build -t busyboxhttpd:v1.1 ./
    
    Sending build context to Docker daemon 2.048 kB
    
    Step 1/5 : FROM busybox
    
     ---> 758ec7f3a1ee
    
    Step 2/5 : LABEL maintainer "hackerlin <hackerlin@hackerlin.com>" app "httpd"
    
     ---> Using cache
    
     ---> ec064f9da09a
    
    Step 3/5 : ENV WEB_DOC_ROOT "/data/web/html"
    
     ---> Using cache
    
     ---> fedfecef57c8
    
    Step 4/5 : RUN mkdir -p ${WEB_DOC_ROOT} &&     echo "<h1>Busybox httpd server</h1>" > ${WEB_DOC_ROOT}/index.html
    
     ---> Running in a9b9f66db1fd
    
     ---> 7ab194ffda82
    
    Removing intermediate container a9b9f66db1fd
    
    Step 5/5 : CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
    
     ---> Running in ecb0430965d0
    
     ---> 3ec2f9b160b7
    
    Removing intermediate container ecb0430965d0
    
    Successfully built 3ec2f9b160b7

     

    3)基于此新建镜像运行容器,进行验证,httpd正常运行  

     

    [root@hackerlin ~]# docker run --name web2 --rm -d busyboxhttpd:v1.1
    
    20aa07198943887db51173e245392f75e3654525cb32242f2b04f0b3e007e47d
    
    [root@hackerlin ~]# docker inspect -f {{.NetworkSettings.IPAddress}} web2
    
    172.10.2
    
    [root@hackerlin ~]# curl 172.10.2
    
    <h1>Busybox httpd server</h1>
    
    --- 也可以使用exec 进入web2容器内进行验证
    
    [root@hackerlin ~]# docker exec -it web2 /bin/sh
    
    / # ps
    
    PID   USER     TIME  COMMAND
    
        1 root      0:00 /bin/httpd -f -h /data/web/html
    
        5 root      0:00 /bin/sh
    
        9 root      0:00 ps
    
    / # printenv
    
    WEB_DOC_ROOT=/data/web/html
    
    / # exit
    
    --- 使用CMD定义的命令,在启动容器时,会被后面追加的指令覆盖;与下面ENTRYPOINT 指令对比
    
    [root@hackerlin ~]# docker kill web2
    
    web2
    
    [root@hackerlin ~]# docker run --name web2 --rm busyboxhttpd:v1.1 ls /
    
    bin
    
    data
    
    dev
    
    etc
    
    [root@hackerlin ~]# curl 172.10.2  被ls /覆盖,所以没有执行httpd服务

    12.1 介绍12 ENTRYPOINT

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

     12.2 语法

     

    ENTR YPOINT <command>
    
    ENTRYPOINT ["<executable>", "<param1>", "<param2>"]

     

    docker run命令传入的命令参数会覆盖CMD指令的内容并且附加到ENTRYPOINT命令最后做为其参数使用注:

    • Dockerfile文件中也可以存在多个ENTRYPOINT指令,但仅有最后一个会生效

    12.3 示例

    1)编写dockerfile文件

     

    FROM busybox
    
    LABEL maintainer="hackerlin <hackerlin@hackerlin.com>" app="httpd"
    
     
    
    ENV WEB_DOC_ROOT="/data/web/html"
    
     
    
    RUN mkdir -p ${WEB_DOC_ROOT} && 
    
        echo "<h1>Busybox httpd server</h1>" > ${WEB_DOC_ROOT}/index.html
    
     
    
    ENTRYPOINT /bin/httpd -f -h ${WEB_DOC_ROOT}

     

    2)使用build 制作镜像  

     

    [root@hackerlin image2]# docker build -t busyboxhttpd:v1.2 ./
    
    Sending build context to Docker daemon 2.048 kB
    
    Step 1/5 : FROM busybox
    
     ---> 758ec7f3a1ee
    
    Step 2/5 : LABEL maintainer "hackerlin <hackerlin@hackerlin.com>" app "httpd"
    
     ---> Using cache
    
     ---> ec064f9da09a
    
    Step 3/5 : ENV WEB_DOC_ROOT "/data/web/html"
    
     ---> Using cache
    
     ---> fedfecef57c8
    
    Step 4/5 : RUN mkdir -p ${WEB_DOC_ROOT} &&     echo "<h1>Busybox httpd server</h1>" > ${WEB_DOC_ROOT}/index.html
    
     ---> Using cache
    
     ---> 7ab194ffda82
    
    Step 5/5 : ENTRYPOINT /bin/httpd -f -h ${WEB_DOC_ROOT}
    
     ---> Running in aebee4cd3d4f
    
     ---> 2e3853256eab
    
    Removing intermediate container aebee4cd3d4f
    
    Successfully built 2e3853256eab

     

    3)基于此新建镜像运行容器,进行验证  

     

    [root@hackerlin ~]# docker run --name web2 --rm busyboxhttpd:v1.2 ls /
    
    发现是不会执行ls / 这个命令;仍然执行的是ENTRYPOINT中设置的命令;与上面CMD 指令对比;
    
    [root@hackerlin ~]# curl 172.10.2   #httpd服务仍然执行,没有被ls / 指令覆盖
    
    <h1>Busybox httpd server</h1>

    13.1 介绍13 HEALTHCHECK

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

    13.2 语法

     

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

     

    --interval=DURATION (default: 30s):每隔多长时间探测一次,默认301OPTIONS 选项:

    • -- timeout= DURATION (default: 30s):服务响应超时时长,默认30
    • --start-period= DURATION (default: 0s):服务启动多久后开始探测,默认0
    • --retries=N (default: 3):认为检测失败几次为宕机,默认3

    2)返回值:

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

     

    13.3 示例

    1)编写dockerfile文件

    检测web2容器的10080端口(其实打开的是80端口,10080并没有,所有会检测失败)

     

    FROM busybox
    
    LABEL maintainer="hackerlin <hackerlin@hackerlin.com>" app="httpd"
    
     
    
    ENV WEB_DOC_ROOT="/data/web/html"
    
     
    
    RUN mkdir -p ${WEB_DOC_ROOT} && 
    
        echo "<h1>Busybox httpd server</h1>" > ${WEB_DOC_ROOT}/index.html
    
     
    
    CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
    
     
    
    HEALTHCHECK --start-period=3s CMD wget -O - -q http://${IP:-0.0.0.0}:10080/

     

      

    2)使用build 制作镜像

     

    [root@docker2 image2]# docker build -t busyboxhttpd:v1.3 ./
    
    Sending build context to Docker daemon  2.048kB
    
    Step 1/6 : FROM busybox
    
     ---> 3a093384ac30
    
    Step 2/6 : LABEL maintainer="hackerlin <hackerlin@hackerlin.com>" app="httpd"
    
     ---> Using cache
    
     ---> 164a0af1b5dc
    
    Step 3/6 : ENV WEB_DOC_ROOT="/data/web/html"
    
     ---> Using cache
    
     ---> 930023fca80b
    
    Step 4/6 : RUN mkdir -p ${WEB_DOC_ROOT} &&     echo "<h1>Busybox httpd server</h1>" > ${WEB_DOC_ROOT}/index.html
    
     ---> Using cache
    
     ---> 1df364fca443
    
    Step 5/6 : CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
    
     ---> Running in 671f9491d0c3
    
    Removing intermediate container 671f9491d0c3
    
     ---> 71451fa629e9
    
    Step 6/6 : HEALTHCHECK --start-period=3s CMD wget -O - -q http://${IP:-0.0.0.0}:${PORT:-80}/
    
     ---> Running in 474395f265da
    
    Removing intermediate container 474395f265da
    
     ---> 757ab550ea25
    
    Successfully built 757ab550ea25
    
    Successfully tagged busyboxhttpd:v1.3

     

    3)基于此新建镜像运行容器,进行验证  

     

    [root@docker2 ~]# docker run --name web2 --rm -d busyboxhttpd:v1.3
    
    7060a6717e77acecd683a6b05332ab7cec128b836e6aa2d7d5ce8fb1b38b7fd6
    
    --- 容器刚创建,还没检测完时,容器还是health 健康状态
    
    [root@docker2 ~]# docker ps
    
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS               NAMES
    
    7060a6717e77        busyboxhttpd:v1.3   "/bin/sh -c '/bin/ht…"   3 seconds ago       Up 3 seconds (health: starting)                       web2
    
    --- 30s 检测完时,没有检测到10080端口,容器变为unhealthy不健康状态
    
    [root@docker2 ~]# docker ps
    
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
    
    7060a6717e77        busyboxhttpd:v1.3   "/bin/sh -c '/bin/ht…"   30 seconds ago      Up 30 seconds (unhealthy)                       web2

    14.1 介绍14 ONBUILD

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

    14.2 语法


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

    1)编写第一个Dockerfile文件,准备作为第二个Dockerfile文件的FROM基础镜像

     

    FROM busybox
    
    MAINTAINER "hackerlin <hackerlin@hackerlin.com>"
    
     
    
    ENV WEB_DOC_ROOT="/data/web/html"
    
     
    
    RUN mkdir -p ${WEB_DOC_ROOT} && 
    
        echo "<h1>Busybox httpd server1</h1>" > ${WEB_DOC_ROOT}/index.html
    
     
    
    ONBUILD RUN echo "<h1>Busybox httpd server2</h1>" >> /data/web/html/index.html

     

    2)编写第2Dockerfile文件,FROM 基于第1Dockerfile  

     

    FROM busyboxhttpd:v2.1

     

    3)基于2Dockerfile文件新建镜像,并运行容器,进行验证 

    ① 基于第1Dockerfile文件新建镜像

     

    [root@hackerlin image3]# docker build -t busyboxhttpd:v2.1 ./
    
    Sending build context to Docker daemon 2.048 kB
    
    Step 1/5 : FROM busybox
    
     ---> 758ec7f3a1ee
    
    Step 2/5 : MAINTAINER "hackerlin <hackerlin@hackerlin.com>"
    
     ---> Using cache
    
     ---> 5d2cda1631d7
    
    Step 3/5 : ENV WEB_DOC_ROOT "/data/web/html"
    
     ---> Running in 210437fc0778
    
     ---> 0a3becd99802
    
    Removing intermediate container 210437fc0778
    
    Step 4/5 : RUN mkdir -p ${WEB_DOC_ROOT} &&     echo "<h1>Busybox httpd server1</h1>" > ${WEB_DOC_ROOT}/index.html &&     /bin/chown nobody ${WEB_DOC_ROOT}/index.html /bin/httpd
    
     ---> Running in e370d8843211
    
     ---> 4c1f59085f78
    
    Removing intermediate container e370d8843211
    
    Step 5/5 : ONBUILD run echo "<h1>Busybox httpd server2</h1>" >> /data/web/html/index.html
    
     ---> Running in f47258319e1d
    
     ---> 232b1c393634
    
    Removing intermediate container f47258319e1d
    
    Successfully built 232b1c393634

     

    ② 基于第2Dockerfile文件新建镜像  

     

    [root@hackerlin ~]# docker build -t busyboxhttpd:v2.2 ./
    
    Sending build context to Docker daemon 2.467 MB
    
    Step 1/1 : FROM busyboxhttpd:v2.1
    
    # Executing 1 build trigger...
    
    Step 1/1 : RUN echo "<h1>Busybox httpd server2</h1>" >> /data/web/html/index.html
    
     ---> Using cache
    
     ---> a2a6ae382228
    
    Successfully built a2a6ae382228

     

    ③ 基于二个新镜像启动容器验证  

     

    [root@hackerlin ~]# docker run --name web2 --rm busyboxhttpd:v2.1 cat /data/web/html/index.html
    
    <h1>Busybox httpd server1</h1>
    
    --- 证明ONBUILD指令,只在第2个Dockerfile文件中生效
    
    [root@hackerlin ~]# docker run --name web2 --rm busyboxhttpd:v2.2 cat /data/web/html/index.html
    
    <h1>Busybox httpd server1</h1>
    
    <h1>Busybox httpd server2</h1>

    15不常用的 dockerfile 指令 

    15.1 USER

    15.1.1 介绍

    • 用于指定运行image时的或运行Dockerfile中任何RUNCMDEntRyPoInT指令指定的程序时的用户名或UID
    • 默认情况下,container的运行身份为root用户

     

    15.1.2 格式

     

    USER <UID>| <U JserName >

     

       需要注意的是,<UID>可以为任意数字,但实践中其必须为/etc/ passwd中某用户的有效UID,否则,docker run命令将运行失败

    15.2 ARG

    15.2.1 介绍

    • ARG指令类似ENV,定义了一个变量;区别于ENV:用户可以在构建时docker build --build-arg <varname> = <value> 进行对变量的修改;ENV不可以;
    • 如果用户指定了未在Dockerfile中定义的构建参数,那么构建输出警告。

     15.2.2 格式

     

    ARG <name>[= <default value>]

    15.2.2 示例Dockerfile可以包含一个或多个ARG指令

    1)在dockerfile 中定义ARG

     

    FROM busybox:latest
    
    ARG author="hackerlin <hackerlin@hackerlin.com>"
    
    MAINTAINER $ARG

     

    2)构建此时镜像时,修改此age  

     

    docker build --build-arg author="Tom <tom@tom.com>" -t test:v0.1 ./

    15.3 SHELL 

    15.3.1 介绍

    • SHELL指令允许覆盖用于shell命令形式的默认shell
    • Linux上的默认shell[“/ bin / sh”“c”],在Windows上是[“cmd”“/ S”“/ C”]
    • SHELL指令必须以JSON格式写入Dockerfile

    15.3.2 语法

    SHELL ["executable", "parameters"]

    15.4 STOPSIGNALSHELL指令可以多次出现。

    每个SHELL指令都会覆盖所有先前的SHELL指令,并影响所有后续指令。

    STOPSIGNAL指令设置将发送到容器出口的系统调用信号。

    此信号可以是与内核的系统调用表中的位置匹配的有效无符号数,例如9,或者SIGNAME格式的信号名,例如SIGKILL

    15.4.1 介绍

    15.4.2 语法

    STOPSIGNAL signal

     

  • 相关阅读:
    团队冲刺第五天
    团队冲刺第一天
    IOS开发(四):开关控件UISwitch
    IOS开发(七):UIActionSheet和UIAlertView
    IOS开发(五):分段控件UISegmentedControl
    [转]IOS开发(一):第一个有交互的APP
    [转]IOS开发(三):UIImageView、UISlider、UIStepper、UIButton
    IOS开发(八):系统声音服务
    [转]Tab Bars和Pickers
    [转]IOS开发(二):ImageView、TextField、键盘
  • 原文地址:https://www.cnblogs.com/hackerlin/p/12699632.html
Copyright © 2020-2023  润新知