• docker--build自己的image


    通过container commit成image

    [root@localhost docker_test]# docker container commit #可以简写成docker commit
    "docker container commit" requires at least 1 and at most 2 arguments.
    See 'docker container commit --help'.
    
    Usage:  docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
    
    Create a new image from a container's changes
    [root@localhost docker_test]# docker image build #可以简写成docker build
    "docker image build" requires exactly 1 argument.
    See 'docker image build --help'.
    
    Usage:  docker image build [OPTIONS] PATH | URL | -
    
    Build an image from a Dockerfile

    以centos image为例,centos base image 默认没安装vim,进入交互式,yum install vim

    [root@localhost docker_test]# docker image ls 
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    bigni/test1         latest              f5620b92331c        2 hours ago         861kB
    ubuntu              14.04               2c5e00d77a67        7 weeks ago         188MB
    centos              latest              9f38484d220f        3 months ago        202MB
    hello-world         latest              fce289e99eb9        6 months ago        1.84kB
    [root@localhost docker_test]# docker run centos
    [root@localhost docker_test]# docker run centos -it
    docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: "-it": executable file not found in $PATH": unknown.
    ERRO[0000] error waiting for container: context canceled 
    [root@localhost docker_test]# docker run -it  centos
    [root@dba873e57276 /]# ls
    anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    [root@dba873e57276 /]# vim
    bash: vim: command not found
    [root@dba873e57276 /]# yum install -y vim
    [root@localhost docker_test]# docker container ls -a #查看所有container
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
    dba873e57276        centos              "/bin/bash"         8 minutes ago       Exited (0) 11 seconds ago                       infallible_rubin
    36c7c2011bdb        centos              "-it"               8 minutes ago       Created                                         wonderful_haslett
    2401371f2c7a        centos              "/bin/bash"         9 minutes ago       Exited (0) 8 minutes ago                        vibrant_dijkstra
    [root@localhost docker_test]# docker commit 
    "docker commit" requires at least 1 and at most 2 arguments.
    See 'docker commit --help'.
    
    Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]  #docker commit 使用方法
    
    Create a new image from a container's changes
    [root@localhost docker_test]# docker commit infallible_rubin bigni/centos_vim #把names:infallible_rubin 的container commit 成image:bigni/centos_vim
    sha256:afe852c481e754b88b28a663f6e98e0a52816e253c64af4b2b823f8bb8702961
    [root@localhost docker_test]# docker image ls -a #查看新增image,比centos image大了好多,安装vim 需要的依赖包不少。
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    bigni/centos_vim    latest              afe852c481e7        10 seconds ago      362MB
    <none>              <none>              3ec8199c2855        3 hours ago         861kB
    bigni/test1         latest              f5620b92331c        3 hours ago         861kB
    ubuntu              14.04               2c5e00d77a67        7 weeks ago         188MB
    centos              latest              9f38484d220f        3 months ago        202MB
    hello-world         latest              fce289e99eb9        6 months ago        1.84kB
    [root@localhost docker_test]# docker run -it bigni/centos_vim #交互式运行
    bigni/centos_vim         bigni/centos_vim:latest  
    [root@localhost docker_test]# docker run -it bigni/centos_vim #交互式运行,测试能不能使用vim
    [root@3afa6597d516 /]# vim test  
    [root@3afa6597d516 /]# cat test
    docker so easy
    [root@3afa6597d516 /]# exit        #退出
    exit
    [root@localhost docker_test]# docker container ls -a #可以看到新增一个container
    CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                      PORTS               NAMES
    3afa6597d516        bigni/centos_vim    "/bin/bash"         About a minute ago   Exited (0) 12 seconds ago                       cranky_zhukovsky
    dba873e57276        centos              "/bin/bash"         12 minutes ago       Exited (0) 4 minutes ago                        infallible_rubin
    36c7c2011bdb        centos              "-it"               12 minutes ago       Created                                         wonderful_haslett
    2401371f2c7a        centos              "/bin/bash"         12 minutes ago       Exited (0) 12 minutes ago                       vibrant_dijkstra
    [root@localhost docker_test]#

    查看原始的centos image 和新增的image

    [root@localhost docker_test]# docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    bigni/centos_vim    latest              afe852c481e7        7 minutes ago       362MB
    bigni/test1         latest              f5620b92331c        3 hours ago         861kB
    ubuntu              14.04               2c5e00d77a67        7 weeks ago         188MB
    centos              latest              9f38484d220f        3 months ago        202MB
    hello-world         latest              fce289e99eb9        6 months ago        1.84kB
    [root@localhost docker_test]# docker history 9f38484d220f
    IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
    9f38484d220f        3 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
    <missing>           3 months ago        /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
    <missing>           3 months ago        /bin/sh -c #(nop) ADD file:074f2c974463ab38c…   202MB               
    [root@localhost docker_test]# docker history afe852c481e7
    IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
    afe852c481e7        7 minutes ago       /bin/bash                                       160MB           #可以看到,比原始的image多了一层 layer    
    9f38484d220f        3 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
    <missing>           3 months ago        /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
    <missing>           3 months ago        /bin/sh -c #(nop) ADD file:074f2c974463ab38c…   202MB               
    [root@localhost docker_test]# 

    上面就是通过docker commit 在base image至少build自己的image,这种方式不值得提倡,因为当把这个image发布出去后,别人并不知该image安装了什么,

    而且也有可能是不安全的image。

    另一种方式是通过Dockerfile来build一个image。

    [root@localhost ~]# mkdir docker_vim
    [root@localhost ~]# vim docker_vim/Dockerfile
    [root@localhost ~]# cat docker_vim/Dockerfile 
    FROM centos
    RUN yum install -y vim
    [root@localhost ~]# docker build -t bigni/centos_vim ./docker_vim/ #通过dockerfile build 新的image
    Sending build context to Docker daemon  2.048kB
    Step 1/2 : FROM centos #from centos
     ---> 9f38484d220f #直接引用centos image这一层,而不会新创建一层
    Step 2/2 : RUN yum install -y vim #运行Dockerfile里配置的命令
     ---> Running in 551ad2fd3374 #创建临时container
    Loaded plugins: fastestmirror, ovl
    Determining fastest mirrors
     * base: mirrors.aliyun.com
     * extras: mirrors.aliyun.com
     * updates: mirrors.aliyun.com
    Resolving Dependencies
    --> Running transaction check
    ---> Package vim-enhanced.x86_64 2:7.4.160-6.el7_6 will be installed
    --> Processing Dependency: vim-common = 2:7.4.160-6.el7_6 for package: 2:vim-enhanced-7.4.160-6.el7_6.x86_64
    中间省略---> Package perl-Pod-Usage.noarch 0:1.63-3.el7 will be installed
    --> Processing Dependency: perl(Pod::Text) >= 3.15 for package: perl-Pod-Usage-1.63-3.el7.noarch
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ================================================================================
     Package                    Arch       Version                Repository   Size
    ================================================================================
    Installing:
     vim-enhanced               x86_64     2:7.4.160-6.el7_6      updates     1.0 M
    Installing for dependencies:
    中间省略which                      x86_64     2.20-7.el7             base         41 k
    
    Transaction Summary
    ================================================================================
    Install  1 Package (+32 Dependent packages)
    
    Total download size: 19 M
    Installed size: 63 M
    Downloading packages:
    warning: /var/cache/yum/x86_64/7/base/packages/gpm-libs-1.20.7-5.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
    Public key for gpm-libs-1.20.7-5.el7.x86_64.rpm is not installed
    Public key for perl-Pod-Escapes-1.04-294.el7_6.noarch.rpm is not installed
    --------------------------------------------------------------------------------
    Total                                              181 kB/s |  19 MB  01:49     
    Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    Importing GPG key 0xF4A80EB5:
     Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
     Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
     Package    : centos-release-7-6.1810.2.el7.centos.x86_64 (@CentOS)
     From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : gpm-libs-1.20.7-5.el7.x86_64                                1/33       中间省略                 
    
    Complete!
    Removing intermediate container 551ad2fd3374
     ---> f853f2a3f901
    Successfully built f853f2a3f901 #创建完成

    查看创建的image

    [root@localhost ~]# docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    bigni/centos_vim    latest              f853f2a3f901        15 minutes ago      362MB
    bigni/test1         latest              f5620b92331c        24 hours ago        861kB
    ubuntu              14.04               2c5e00d77a67        7 weeks ago         188MB
    centos              latest              9f38484d220f        3 months ago        202MB
  • 相关阅读:
    Java练习 SDUT-1117_求绝对值(选择结构)
    Java练习 SDUT-2561_九九乘法表
    Java练习 SDUT-1160_某年某月的天数
    HDU-1024_Max Sum Plus Plus
    博客园页面DIY
    JDBC
    JavaSE | Lambda| Optional| Stream API
    JavaSE| 网络编程
    JavaSE| 反射
    JavaSE | IO流
  • 原文地址:https://www.cnblogs.com/laonicc/p/11143683.html
Copyright © 2020-2023  润新知