• Docker


    Dockerfile

    Docker Hub拥有大量高质的官方镜像:可直接使用的服务类镜像、语言应用镜像、基础操作系统镜像等,满足绝大部分需求。
    此外,可以通过定制镜像的方式来满足实际使用中的特定需求。

    定制镜像实际上就是以一个镜像为基础,定制每一层的配置和文件。
    可以选择现有镜像为基础镜像,也可以选择scratch镜像(虚拟的概念,并不实际存在,表示一个空白的镜像)。

    Dockerfile是包含了新镜像创建过程中的每一层修改、安装、构建、操作指令的文本格式脚本。每一条指令(Instruction)构建一层,描述该层应当如何构建。
    Dockerfile支持的换行方式和行首#的注释格式。良好的指令格式会让维护和排障更简便。

    镜像是多层存储,每一层的内容并不会在下一层被删除,会一直伴随镜像。
    因此镜像构建时,确保每一层只添加真正需要的内容,清理任何无关的文件和配置。

    Dockerfile的常用指令

    Dockerfile中的指令是通过docker build命令来执行的。
    更多信息可参考:Dockerfile reference

    - FROM 指定基础镜像。
        Dockerfile中的必备指令,也是第一条指令。
        “FROM scratch”表示选择空白镜像为基础镜像,所写的指令将作为镜像第一层开始存在,适合Linux下静态编译的程序,会让镜像体积更加小巧。
    - RUN 执行命令。
        每一个RUN指令都会新建立一层,在这一层执行对应的命令,然后commit这一层的修改,构成新的镜像。
        为了简洁快速构建和满足低于Union FS的最大层数限制,可以在一个RUN指令里使用`&&`串联多个命令,从而将多层简化为一层。
    

    docker build

    Build an image from a Dockerfile
    Usage:	docker build [OPTIONS] PATH | URL | -
    

    构建镜像的步骤

    1. 确定镜像模板
    2. 新建Dockerfile文件
    3. 使用Dockerfile的指令完善Dockerfile的内容
    4. 在Dockerfile文件的所在路径执行docker build -t imageName:tag .
    5. 执行docker run命令并指定imageName:tag和相关参数即可

    注意事项

    1. 必须使用PATH参数指定上下文目录。
      Docker是C/S结构,执行docker build命令其实是在服务端(Docker引擎)中构建的,而不是在本地构建。
      镜像构建时,通过指定上下文目录的方式,可以让服务端获得本地文件,从而能够构建包含本地文件的镜像。简而言之,需要加入镜像的本地文件必须存在于上下文目录中。
      docker build命令会将用户指定的上下文目录的内容打包并上传给Docker引擎。Docker引擎接收并展开就可以获得构建镜像所需的本地文件。
    2. 在上下文目录中,可以通过.dockerignore(语法类似.gitignore)排除不需要加入镜像的内容。
    3. COPY、ADD等指令中的源文件路径,使用的都是上下文(PATH)目录的相对路径。
    4. 如不指定dockerfile,Docker默认文件名为Dockerfile并放置于上下文目录中。

    其它用法

    • 从URL构建
    • 用给定的tar压缩包构建:Docker引擎下载压缩包并自动解压缩,以其作为上下文开始构建。
    • 从标准输入中读取Dockerfile进行构建(缺少上下文,无法执行COPY等指令):docker build - < Dockerfilecat Dockerfile | docker build -
    • 从标准输入中读取上下文压缩包进行构建:docker build - < context.tar.gz

    示例

    编写dockfile

    [root@CentOS7 docker]# pwd
    /tmp/docker
    [root@CentOS7 docker]# ls -l
    总用量 0
    drwxr-xr-x 2 root root 24 5月   8 23:52 data
    drwxr-xr-x 3 root root 60 5月   9 00:13 test
    [root@CentOS7 docker]# tree
    .
    ├── data
    │   └── sample.txt
    └── test
        ├── aliyun-sources.txt
        ├── buildfile
        └── dir
            └── messages.txt
    
    3 directories, 4 files
    [root@CentOS7 docker]# 
    [root@CentOS7 docker]# cat /tmp/docker/data/sample.txt 
    1234567890
    [root@CentOS7 docker]# 
    [root@CentOS7 docker]# cat /tmp/docker/test/aliyun-sources.txt 
    deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
    deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
    deb http://mirrors.aliyun.com/ubuntu/ xenial universe
    deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
    deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
    deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
    deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
    deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
    deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
    [root@CentOS7 docker]# 
    [root@CentOS7 docker]# cat /tmp/docker/test/buildfile
    # this is an example of Docker build. 
    FROM ubuntu
    MAINTAINER anliven "anliven@yeah.net"
    RUN echo 'Run docker build - create file!' > /tmp/file-create.log
    COPY aliyun-sources.txt /etc/apt/sources.list
    COPY dir/messages.txt /tmp/file-copy.log
    VOLUME /tmp/docker/data
    # ENV http_proxy="http://10.144.1.10:8080"
    RUN apt-get update 
        && apt-get install -y inetutils-ping iproute net-tools 
        && apt-get install -y vim 
        && apt-get purge -y --auto-remove
    [root@CentOS7 docker]# 
    [root@CentOS7 docker]# cat /tmp/docker/test/dir/messages.txt 
    Run docker build - copy file!
    [root@CentOS7 docker]# 
    

    定制镜像

    [root@CentOS7 ~]# docker build --file /tmp/docker/test/buildfile --tag ubuntu:test /tmp/docker/test/
    Sending build context to Docker daemon  5.12 kB
    Step 1 : FROM ubuntu
     ---> f7b3f317ec73
    Step 2 : MAINTAINER anliven <anliven@yeah.net>
     ---> Running in 7c1140a0cc72
     ---> 9a0fb45df847
    Removing intermediate container 7c1140a0cc72
    Step 3 : RUN echo 'Run docker build - create file!' > /tmp/file-create.log
     ---> Running in 33bb7a725234
     ---> 866235e56f75
    Removing intermediate container 33bb7a725234
    Step 4 : COPY aliyun-sources.txt /etc/apt/sources.list
     ---> 6de0504452f5
    Removing intermediate container e98687ed3e37
    Step 5 : COPY dir/messages.txt /tmp/file-copy.log
     ---> 8def1507d4f3
    Removing intermediate container 66e68d3efc2d
    Step 6 : VOLUME /tmp/docker/data
     ---> Running in 10cca5bef10e
     ---> 6e887fff9079
    Removing intermediate container 10cca5bef10e
    Step 7 : RUN apt-get update     && apt-get install -y inetutils-ping iproute net-tools     && apt-get install -y vim     && apt-get purge -y --auto-remove
     ---> Running in 4c54d21066cd
    Get:1 http://mirrors.aliyun.com/ubuntu xenial InRelease [247 kB]
    Get:2 http://mirrors.aliyun.com/ubuntu xenial-updates InRelease [102 kB]
    Get:3 http://mirrors.aliyun.com/ubuntu xenial-backports InRelease [102 kB]
    ......
    ......
    ......
    Processing triggers for libc-bin (2.23-0ubuntu7) ...
    Reading package lists...
    Building dependency tree...
    Reading state information...
    0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.
     ---> 62b945705a30
    Removing intermediate container 4c54d21066cd
    Successfully built 62b945705a30
    [root@CentOS7 ~]# 
    

    验证

    [root@CentOS7 ~]# docker images ubuntu
    REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
    ubuntu              test                62b945705a30        About a minute ago   202.1 MB
    docker.io/ubuntu    latest              f7b3f317ec73        13 days ago          117.3 MB
    [root@CentOS7 ~]# 
    [root@CentOS7 ~]# docker inspect --format "{{ .Config.Volumes }}" ubuntu:test
    map[/tmp/docker/data:{}]
    [root@CentOS7 ~]# 
    [root@CentOS7 ~]# docker history ubuntu:test
    IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
    62b945705a30        5 minutes ago       /bin/sh -c apt-get update     && apt-get inst   84.85 MB            
    6e887fff9079        6 minutes ago       /bin/sh -c #(nop)  VOLUME [/tmp/docker/data]    0 B                 
    8def1507d4f3        6 minutes ago       /bin/sh -c #(nop) COPY file:45739c777f02cabe8   30 B                
    6de0504452f5        6 minutes ago       /bin/sh -c #(nop) COPY file:3d19f8187c6e93e48   655 B               
    866235e56f75        6 minutes ago       /bin/sh -c echo 'Run docker build - create fi   32 B                
    9a0fb45df847        6 minutes ago       /bin/sh -c #(nop)  MAINTAINER anliven <anlive   0 B                 
    f7b3f317ec73        13 days ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0 B                 
    <missing>           13 days ago         /bin/sh -c mkdir -p /run/systemd && echo 'doc   7 B                 
    <missing>           13 days ago         /bin/sh -c sed -i 's/^#s*(deb.*universe)$/   2.759 kB            
    <missing>           13 days ago         /bin/sh -c rm -rf /var/lib/apt/lists/*          0 B                 
    <missing>           13 days ago         /bin/sh -c set -xe   && echo '#!/bin/sh' > /u   745 B               
    <missing>           13 days ago         /bin/sh -c #(nop) ADD file:141408db9037263a47   117.3 MB            
    [root@CentOS7 ~]# 
    [root@CentOS7 ~]# docker run -it ubuntu:test
    root@702e453e458f:/#   
    root@702e453e458f:/# ls -l /tmp
    total 8
    drwxr-xr-x 3 root root 18 May  8 16:33 docker
    -rw-r--r-- 1 root root 30 May  8 15:53 file-copy.log
    -rw-r--r-- 1 root root 32 May  8 16:33 file-create.log
    root@702e453e458f:/# 
    root@702e453e458f:/# cat /tmp/file-copy.log 
    Run docker build - copy file!
    root@702e453e458f:/# cat /tmp/file-create.log 
    Run docker build - create file!
    root@702e453e458f:/# 
    root@702e453e458f:/# ls -l /tmp/docker/
    total 0
    drwxr-xr-x 2 root root 6 May  8 16:40 data
    root@702e453e458f:/# ls -l /tmp/docker/data/
    total 0
    root@702e453e458f:/# 
    root@702e453e458f:/# dpkg --list inetutils-ping |grep ii
    ii  inetutils-ping 2:1.9.4-1build1 amd64        ICMP echo tool
    root@702e453e458f:/# 
    root@702e453e458f:/# dpkg --list iproute |grep ii
    ii  iproute        1:4.3.0-1ubuntu3 all          transitional dummy package for iproute2
    root@702e453e458f:/# 
    root@702e453e458f:/# dpkg --list net-tools |grep ii
    ii  net-tools      1.60-26ubuntu1 amd64        NET-3 networking toolkit
    root@702e453e458f:/# 
    root@702e453e458f:/# exit
    [root@CentOS7 ~]# 
    

    参考

  • 相关阅读:
    Eclipse安装SVN插件(转载)
    推荐eclipse插件Properties Editor(转)
    Eclipse快捷键(转载)
    添加路由命令
    oracle 随机数(转载)
    《图解CSS3——第1章 揭开CSS3的面纱》
    css高度自适应
    JS生成随机数
    判断IE浏览器版本
    IE下SCRIPT5009: “JSON”未定义
  • 原文地址:https://www.cnblogs.com/anliven/p/6828565.html
Copyright © 2020-2023  润新知