一、根据 Container 创建 Image

通过这种方式创建的Image,别人是不知道这个Image是如何产生的,很容易担心里面是否有恶意软件,
所以我们不提倡这种方式创建的Image,因为这种方式不能保证Image的安全性

➜  ~ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              470671670cac        5 weeks ago         237MB

➜  ~ docker run -it centos
[root@575d4903cab7 /]# exit

➜  ~ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS         NAMES
575d4903cab7        centos              "/bin/bash"              35 minutes ago      Exited (0) 14 seconds ago                 pensive_blackburn

➜  ~ docker commit pensive_blackburn hhcycj/centos-vim       # 注意:通过这个命令生成的Image
sha256:ff2dbd3c570c67724d35693df34519dd9c18b9f80fa3512fb64c8e2f03be8839

➜  ~ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hhcycj/centos-vim   latest              ff2dbd3c570c        11 seconds ago      300MB
centos              latest              470671670cac        5 weeks ago         237MB

➜  ~ docker history ff2dbd3c570c
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
ff2dbd3c570c        52 seconds ago      /bin/bash                                       63MB                
470671670cac        5 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           5 weeks ago         /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
<missing>           5 weeks ago         /bin/sh -c #(nop) ADD file:aa54047c80ba30064…   237MB               
➜  ~

二、通过 Dockerfile 创建 Image

通过Dockerfile创建的镜像,人们能看清楚这个镜像的创建过程,所以相对是更安全的

➜  test pwd
/Users/hxs/test
➜  mkdir docker-centos-vim
➜  cd docker-centos-vim


➜  vim Dockerfile
 1 FROM centos
 2 RUN yum install -y vim


➜  docker build -t hhcycj/centos-vim-new .       # 注意:. 代表是当前目录下的Dockerfile来Build镜像
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM centos
 ---> 470671670cac
Step 2/2 : RUN yum install -y vim
 ---> Running in 4bb33c55a439  # 生成临时的layer
 ......                        # 省略显示安装vim的过程
Installed:
  vim-enhanced-2:8.0.1763-13.el8.x86_64 gpm-libs-1.20.7-15.el8.x86_64
  vim-common-2:8.0.1763-13.el8.x86_64   vim-filesystem-2:8.0.1763-13.el8.noarch
  which-2.21-10.el8.x86_64

Complete!
Removing intermediate container 4bb33c55a439  # 删除临时产生的layer
 ---> e731b226f636
Successfully built e731b226f636               # 最终生成的镜像layer
Successfully tagged hhcycj/centos-vim-new:latest


➜  docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
hhcycj/centos-vim-new   latest              e731b226f636        24 seconds ago      300MB
nginx                   latest              2073e0bcb60e        3 weeks ago         127MB
centos                  latest              470671670cac        5 weeks ago         237MB
elasticsearch           latest              5acf0e8da90b        17 months ago       486MB