• Docker系列-4.构建镜像


    Docker系列-4.构建镜像

    创建镜像的既可以在已有的镜像上修改创建,也可以从头开始构建一个全新的镜像。

    使用 docker commit 命令可以在已有的镜像上修改创建出新的镜像,创建之前首先要理解docker分层的概念

    Docker镜像分层

    每个镜像都是由多个镜像层组成的,镜像层中每一层都是只读的,以栈的方式组合在一起组成容器的根文件系统。而在

    使用docker history查看image的各个layer

    [root@localhost ~]# docker history ubuntu
    IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
    1e4467b07108        8 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
    <missing>           8 days ago          /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
    <missing>           8 days ago          /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   811B                
    <missing>           8 days ago          /bin/sh -c [ -z "$(apt-get indextargets)" ]     1.01MB              
    <missing>           8 days ago          /bin/sh -c #(nop) ADD file:65a1cc50a9867c153…   72.9MB              
    [root@localhost ~]# docker images
    

    使用dive工具查看image分层

    持久化容器生成镜像

    通过 docker commit 命令可以提交对容器作出的修改,并创建一个新镜像,这有点像为容器保存一个快照。

    实例化镜像并修改

    [root@localhost ~]# docker run -it ubuntu /bin/bash
    root@c8feedc715e6:/# apt-get update
    Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]            
    ......
    Fetched 14.3 MB in 19s (747 kB/s)                                                                                                                     
    Reading package lists... Done
    root@c8feedc715e6:/# exit
    exit
    
    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                           PORTS               NAMES
    c8feedc715e6        ubuntu              "/bin/bash"              About a minute ago   Exited (0) 13 seconds ago                            practical_curran
    d0058bbeea30        httpd               "httpd-foreground"       5 days ago           Exited (0) 5 days ago                                sharp_hoover
    e40604d8e4cd        nginx               "/docker-entrypoint.…"   5 days ago           Created                                              focused_kirch
    4a10d126524f        ubuntu              "bash"                   5 days ago           Exited (255) About an hour ago                       brave_goldwasser
    8fb045bce0da        hello-world         "/hello"                 5 days ago           Exited (0) 5 days ago                                flamboyant_lichterman
    
    

    提交容器变更到镜像文件

    ##提交容器变更到镜像文件
    [root@localhost ~]# docker commit c8feedc715e6 ubuntu:update
    sha256:3c7832c9920b1d9aec4bbab7e7638e119a6da0570c700a2a055c9a958f435768
    
    [root@localhost ~]# docker images           
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    ubuntu              update              3c7832c9920b        22 seconds ago      96.4MB
    ubuntu              latest              1e4467b07108        8 days ago          73.9MB
    httpd               latest              9d2a0c6e5b57        10 days ago         166MB
    nginx               latest              8cf1bfb43ff5        11 days ago         132MB
    hello-world         latest              bf756fb1ae65        7 months ago        13.3kB
    
    [root@localhost ~]# docker history 3c7832c9920b
    IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
    3c7832c9920b        13 minutes ago      /bin/bash                                       22.6MB              
    1e4467b07108        8 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
    <missing>           8 days ago          /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
    <missing>           8 days ago          /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   811B                
    <missing>           8 days ago          /bin/sh -c [ -z "$(apt-get indextargets)" ]     1.01MB              
    <missing>           8 days ago          /bin/sh -c #(nop) ADD file:65a1cc50a9867c153…   72.9MB              
    

    此时就可以基于刚创建的 ubuntu:update 镜像启动新的容器。

    使用Dockerfile创建镜像

    Dockerfile

    Dockerfile是自动化创建新镜像的指令脚本文件,采用YML格式,由docker build执行。

    Dockerfile指令

    Dockerfile指令即Dockerfile文件中的关键字,每一条指令执行后都会产生一个分层。

    Docker build

    [root@localhost ~]# docker build --help
    
    Usage:  docker build [OPTIONS] PATH | URL | -
    Build an image from a Dockerfile
    

    在Dockerfile文件所在目录下执行docker build -t imageName:tag .

    构建Redis镜像

    [root@localhost redis]# cat Dockerfile 
    FROM centos
    RUN ["yum" , "install" , "-y" ,"gcc","gcc-c++","net-tools","make"]
    WORKDIR /usr/local
    ADD redis-4.0.14.tar.gz .
    WORKDIR /usr/local/redis-4.0.14/src
    RUN make && make install
    WORKDIR /usr/local/redis-4.0.14
    ADD redis-7000.conf .
    EXPOSE 7000
    CMD ["redis-server","redis-7000.conf"]
    
    [root@localhost redis]# docker build -t redis:4.0.14_man .
    Sending build context to Docker daemon  1.745MB
    Step 1/10 : FROM centos
     ---> 831691599b88
    Step 2/10 : RUN ["yum" , "install" , "-y" ,"gcc","gcc-c++","net-tools","make"]
     ---> Running in 759cd5943ce6
    Removing intermediate container 759cd5943ce6
     ---> fd38bdc0ae61
    Step 3/10 : WORKDIR /usr/local
    Removing intermediate container c80c5dd3a4a4
     ---> dc55a6f5baa4
    Step 4/10 : ADD redis-4.0.14.tar.gz .
     ---> 2695590345d1
    Step 5/10 : WORKDIR /usr/local/redis-4.0.14/src
    Removing intermediate container e6090aec2eb4
     ---> 3d23c6fa17c2
    Step 6/10 : RUN make && make install
     ---> Running in b2b0db9554ff
    Removing intermediate container b2b0db9554ff
     ---> c719401c0f14
    Step 7/10 : WORKDIR /usr/local/redis-4.0.14
    Removing intermediate container 43376035527b
     ---> 2d9d3d7bf714
    Step 8/10 : ADD redis-7000.conf .
     ---> fa5a5f6c0f0c
    Step 9/10 : EXPOSE 7000
     ---> Running in 5cdd65243ad7
    Removing intermediate container 5cdd65243ad7
     ---> 4aa95d591516
    Step 10/10 : CMD ["redis-server","redis-7000.conf"]
     ---> Running in c9db88798322
    Removing intermediate container c9db88798322
     ---> 508b9f6e5405
    Successfully built 508b9f6e5405
    Successfully tagged redis:4.0.14_man
    

    验证镜像

    [root@localhost redis]# docker images          
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    redis               4.0.14_man          508b9f6e5405        6 minutes ago       497MB
    ubuntu              update              3c7832c9920b        2 hours ago         96.4MB
    ubuntu              latest              1e4467b07108        9 days ago          73.9MB
    httpd               latest              9d2a0c6e5b57        10 days ago         166MB
    nginx               latest              8cf1bfb43ff5        11 days ago         132MB
    centos              latest              831691599b88        6 weeks ago         215MB
    hello-world         latest              bf756fb1ae65        7 months ago        13.3kB
    
    
    [root@localhost ~]# docker history redis:4.0.14_man
    IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
    508b9f6e5405        26 minutes ago      /bin/sh -c #(nop)  CMD ["redis-server" "redi…   0B                  
    4aa95d591516        26 minutes ago      /bin/sh -c #(nop)  EXPOSE 7000                  0B                  
    fa5a5f6c0f0c        26 minutes ago      /bin/sh -c #(nop) ADD file:5655994a11abcd7dd…   23B                 
    2d9d3d7bf714        26 minutes ago      /bin/sh -c #(nop) WORKDIR /usr/local/redis-4…   0B                  
    c719401c0f14        26 minutes ago      /bin/sh -c make && make install                 103MB               
    3d23c6fa17c2        28 minutes ago      /bin/sh -c #(nop) WORKDIR /usr/local/redis-4…   0B                  
    2695590345d1        28 minutes ago      /bin/sh -c #(nop) ADD file:72d62441fd447fe67…   7.19MB              
    dc55a6f5baa4        28 minutes ago      /bin/sh -c #(nop) WORKDIR /usr/local            0B                  
    fd38bdc0ae61        28 minutes ago      yum install -y gcc gcc-c++ net-tools make       172MB               
    831691599b88        6 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
    <missing>           6 weeks ago         /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
    <missing>           6 weeks ago         /bin/sh -c #(nop) ADD file:84700c11fcc969ac0…   215MB               
    
    

    验证容器

    [root@localhost ~]# docker run -it redis:4.0.14_man /bin/bash
    [root@be61a506b62f redis-4.0.14]# pwd
    /usr/local/redis-4.0.14
    
    [root@be61a506b62f redis-4.0.14]# ls
    00-RELEASENOTES  CONTRIBUTING  INSTALL    Makefile   deps             redis.conf  runtest-cluster   sentinel.conf  tests
    BUGS             COPYING       MANIFESTO  README.md  redis-7000.conf  runtest     runtest-sentinel  src            utils
    
    [root@be61a506b62f redis-4.0.14]# cat /etc/redhat-release 
    CentOS Linux release 8.2.2004 (Core) 
    
    [root@localhost ~]# docker run -itd redis:4.0.14_man
    9728f3469133f5b0c574ee7568c68c7a088dbf06cfd965c0c717f89c31222558
    
    [root@localhost ~]# ps -ef |grep redis
    root     12385 12374  0 12:06 pts/0    00:00:00 redis-server 0.0.0.0:7000
    
  • 相关阅读:
    微信小程序保存图片功能实现
    小程序清除缓存功能如何实现
    小程序自定义函数—数字千位转换
    小程序身份证号检测函数
    小程序 的 textarea 组件 层级问题如何解决
    Markdown 语法背一下咯
    跨域了解一下?
    sort命令的k选项大讨论【转】
    Shell脚本中实现切换用户并执行命令操作【转】
    Ansible Tower系列 四(使用tower执行一个命令)【转】
  • 原文地址:https://www.cnblogs.com/elfcafe/p/13436889.html
Copyright © 2020-2023  润新知