美文网首页
dockerfile指令说明

dockerfile指令说明

作者: 何妨吟啸且徐行1 | 来源:发表于2020-01-08 21:18 被阅读0次
    • 第一条指令必须指定基础镜像
      FROM imageA:tag

    • RUN 指定运行的命令
      一般是基于基础容器,搭建基础环境的操作,属于build image的时候操作的;使用RUN指令的时候,最好不要写多层RUN,使用下面一种形式,\分隔换行继续写就行了,不然层级增加,不利于镜像可读性和简约性
      RUN direct-command
      RUN ["executed-file","param1","param2"]

    • CMD 指令只出容器启动时需要的命令
      与RUN指令的使用实际不同;这里使用可执行文件传参数的时候,要注意使用双引号,参数传递以后,docker会解析成json串
      CMD ["executed-file","param1","param2"]
      CMD direct-command parames

    • LABEL 添加镜像标签
      LABEL key=value key2=value2

    • MAINTAINER指定作者
      MAINTAINER author

    • EXPOSE 对外端口
      EXPOSE端口指定的是外部访问该容器的端口,与主机端口无关,如需要添加映射关系,使用-P参数
      EXPOSE 7171

    • 设置环境变量
      ENV key=value key2=value2

    • ADD 添加文件到镜像中
      可以使用容器内的绝对路径,也可以使用相对路径;file的格式原则上是不限制的,使用过程中可以根据实际情况评估
      ADD filename/url path

    • COPY 拷贝文件
      COPY filename path

    • ENTERPOINT
      启动时的默认命令,用法与RUN和CMD一样
      如果同时写了ENTERPOINT和CMD,二者可能会相互覆盖,,?

    • VOLUME挂载
      一般多设置于需要持久化存储

    • USER容器启动用户
      指定运行命令的层,执行命令的身份
      USER user_name

    • WORKDIR 工作目录
      如果不存在会创建;存在以后多设置几次也ok
      WORKDIR path

    • ARG设置变量

    • ONBUILD 基于当前镜像生成的子镜像才会有的动作
      例如 ONBUILD CMD direct-command parames 这种

    • STOPSIGNAL 容器退出时的系统指令

    • HEALTHCHECK 健康检查

    >> 示例<<

    FROM centos:7.1
    MAINTAINER M_G
    LABEL im_name="my_centos7"\
          im_desc="centos7.1byM_G"\
          im_version="1.0"
    RUN /bin/echo "Testing-RUN1"\
        /bin/echo "Testing-RUN2"\
        /bin/echo "Testing-RUN3"\
        /bin/echo "The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile"
    RUN ["/bin/echo", "-c", "OtherMeth"]
    EXPOSE 7788
    CMD /bin/echo "CMD"
    

    >> 示例运行结果 <<

    docker build -t runtest/centos:7.1 .
    Sending build context to Docker daemon  2.048kB
    Step 1/7 : FROM centos:7.2.1511
     ---> 9aec5c5fe4ba
    Step 2/7 : MAINTAINER M_G
     ---> Running in fd9773ab5d04
    Removing intermediate container fd9773ab5d04
     ---> 5d049cf44ade
    Step 3/7 : LABEL im_name="my_centos7"      im_desc="centos7.1byM_G"   im_version="1.0"
     ---> Running in dbe78be6937a
    Removing intermediate container dbe78be6937a
     ---> 835de35557c0
    Step 4/7 : RUN /bin/echo "Testing-RUN1" /bin/echo "Testing-RUN2"    /bin/echo "Testing-RUN3"    /bin/echo "The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile"
     ---> Running in 200e155ce261
    Testing-RUN1 /bin/echo Testing-RUN2 /bin/echo Testing-RUN3 /bin/echo The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile
    Removing intermediate container 200e155ce261
     ---> 6b20860bc949
    Step 5/7 : RUN ["/bin/echo", "-c", "OtherMeth"]
     ---> Running in 39064900f238
    -c OtherMeth
    Removing intermediate container 39064900f238
     ---> 47e32cba5448
    Step 6/7 : EXPOSE 7788
     ---> Running in c658c7ba76ea
    Removing intermediate container c658c7ba76ea
     ---> e76255a7950f
    Step 7/7 : CMD /bin/echo "CMD"
     ---> Running in 9ab1ff69c36d
    Removing intermediate container 9ab1ff69c36d
     ---> 91f66f06b528
    Successfully built 91f66f06b528
    Successfully tagged runtest/centos:7.1
    

    完成以上指令,docker images已经创建成功,测试一下:

    docker run -t -i runtest/centos:7.1 /bin/echo "TEST"
    # output
    TEST
    

    相关文章

      网友评论

          本文标题:dockerfile指令说明

          本文链接:https://www.haomeiwen.com/subject/octynctx.html