• docker笔记之Dockerfile体系结构


    Dockerfile体系结构(保留字指令)

    FROM:基础镜像,当前镜像是基于哪个镜像的
    MAINTAINER:镜像维护者的姓名和邮箱地址
    RUN:容器构件时需要运行的命令
    EXPOSE:当前容器对外暴露出的端口
    WORKDIR:指定在创建容器后,终端默认登陆进来的工作目录
    ENV:用来在构建镜像过程中设置环境变量
    ADD:将宿主机目录下的文件拷贝进镜像且ADD命令会自动处理URL和解压tar压缩包
    COPY:类似ADD,拷贝文件和目录到镜像中。将从构建上下文目录中<源路径>的文件/目录复制到新的一层的镜像内的<目标路径>位置
    VOLUME:容器数据卷,用于数据保存和持久化工作
    CMD:指定一个容器启动时要执行的命令,dockerfile中可以存在多个CMD,但是只有最后一个生效,前面会被覆盖
    ENTRYPOINT:指定一个容器启动时要执行的命令,和CMD一样,都是在指定容器启动程序和参数
    ONBUILD:当构建一个被继承的Dockerfile时运行命令,父镜像在被子镜像继承后父镜像的onbuild被触发

    示例

    01根据centos的官方镜像创建一个查询自己出口IP的容器

    [root@heibing 20190911]# cat Dockerfile 
    FROM centos
    RUN yum install curl -y
    CMD ["curl","-s","https://ip.cn/"]
    [root@heibing 20190911]# docker build -f Dockerfile -t myip .
    Sending build context to Docker daemon  2.048kB
    Step 1/3 : FROM centos
     ---> 67fa590cfc1c
    Step 2/3 : RUN yum install curl -y
     ---> Running in fe556a2f7dd5
    Loaded plugins: fastestmirror, ovl
    Determining fastest mirrors
     * base: mirror.jdcloud.com
     * extras: mirrors.huaweicloud.com
     * updates: mirror.jdcloud.com
    Package curl-7.29.0-51.el7_6.3.x86_64 already installed and latest version
    Nothing to do
    Removing intermediate container fe556a2f7dd5
     ---> 308ad7c5bc9d
    Step 3/3 : CMD ["curl","-s","https://ip.cn/"]
     ---> Running in e3fb5e4ea34e
    Removing intermediate container e3fb5e4ea34e
     ---> 71438add7273
    Successfully built 71438add7273
    Successfully tagged myip:latest
    [root@heibing 20190911]# docker run -it myip
    {"ip": "223.71.81.68", "country": "北京市", "city": "移动"}
    
    #CMD覆盖问题
    [root@heibing 20190911]# docker run -it myip ls -l
    total 56
    -rw-r--r--.  1 root root 12090 Aug  1 01:10 anaconda-post.log
    lrwxrwxrwx.  1 root root     7 Aug  1 01:09 bin -> usr/bin
    drwxr-xr-x.  5 root root   360 Sep 11 17:18 dev
    drwxr-xr-x.  1 root root  4096 Sep 11 17:18 etc
    #从这里可以看出,CMD命令可以被run后面的参数覆盖,导致预设的命令失效
    
    

    02将test.txt拷贝至镜像中,并在启动容器时输出文件内容

    [root@heibing 20190911]# cat Dockerfile1
    FROM centos
    RUN yum install curl -y
    COPY test.txt /tmp/
    CMD ["cat","/tmp/test.txt"]
    [root@heibing 20190911]# cat test.txt 
    Hello,World~~
    [root@heibing 20190911]# docker build -f Dockerfile1 -t my_test .
    Sending build context to Docker daemon  4.096kB
    Step 1/4 : FROM centos
     ---> 67fa590cfc1c
    Step 2/4 : RUN yum install curl -y
     ---> Using cache
     ---> 308ad7c5bc9d
    Step 3/4 : COPY test.txt /tmp/
     ---> Using cache
     ---> dd18f6d40c2f
    Step 4/4 : CMD ["cat","/tmp/test.txt"]
     ---> Running in c3e9867de724
    Removing intermediate container c3e9867de724
     ---> d934ab117265
    Successfully built d934ab117265
    Successfully tagged my_test:latest
    [root@heibing 20190911]# docker run -it my_test
    Hello,World~~
    
    

    03通过ADD命令将压缩包内容拷贝至镜像中

    #通过for循环生成10个文件
    [root@heibing 20190911]# for i in `seq 10`;do touch $i.txt;done
    [root@heibing 20190911]# ls
    1,10.txt  1.txt  10.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt  Dockerfile  Dockerfile1  test.txt
    #将文件打包
    [root@heibing 20190911]# tar zcvf test.tar.gz ./*.txt
    ./1,10.txt
    ./1.txt
    ./10.txt
    ./2.txt
    ./3.txt
    ./4.txt
    ./5.txt
    ./6.txt
    ./7.txt
    ./8.txt
    ./9.txt
    ./test.txt
    [root@heibing 20190911]# ls
    1,10.txt  1.txt  10.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt  Dockerfile  Dockerfile1  test.tar.gz  test.txt
    #编写Dockerfile文件
    [root@heibing 20190911]# cat Dockerfile2
    FROM centos
    ADD test.tar.gz /tmp/
    CMD ["ls","/tmp"]
    #生成镜像
    [root@heibing 20190911]# docker build -f Dockerfile2 -t my_test .
    Sending build context to Docker daemon   12.8kB
    Step 1/3 : FROM centos
     ---> 67fa590cfc1c
    Step 2/3 : ADD test.tar.gz /tmp/
     ---> a38cc88f9d06
    Step 3/3 : CMD ["ls","/tmp"]
     ---> Running in 38d615919937
    Removing intermediate container 38d615919937
     ---> 0b1fc9cc784f
    Successfully built 0b1fc9cc784f
    Successfully tagged my_test:latest
    #启动容器查看结果
    [root@heibing 20190911]# docker run -it my_test
    1,10.txt  10.txt  3.txt  5.txt	7.txt  9.txt		 test.txt
    1.txt	  2.txt   4.txt  6.txt	8.txt  ks-script-rnBCJB  yum.log
    
  • 相关阅读:
    Help Hanzo (素数筛+区间枚举)
    How to create and apply a patch with Git
    Assembly
    GNU C 内联汇编介绍
    Arch Linux sudo: PAM authentication error: Module is unknown [Solved!]
    关于 RTL8723BS 同时开启 STA/AP 模式
    单源最短路径算法——Dijkstra算法
    Altium Designer PCB制作入门实例
    I2C 总线协议
    TarJan 算法求解有向连通图强连通分量
  • 原文地址:https://www.cnblogs.com/cangyuefeng/p/11506859.html
Copyright © 2020-2023  润新知