Docker

利用docker commit命令生成镜像

Docker镜像是多层存储,每一层是在前一层的基础上进行的修改。而容器是镜像为基础层的多层存储。
如果不使用数据卷,运行一个容器的时候,对任何文件的修改都会被记录于容器存储层。
docker commit 命令可以将容器存储层保存下来成为镜像。也就将原有镜像的基础层和容器存储层,并构成包含原有容器最后的文件变化的新镜像。

但在实际环境中,推荐使用Dockerfile来完成定制镜像,而不是使用docker commit 命令。
原因如下:

  • 大量的无关内容被添加进来,如果不小心清理,将会导致镜像极为臃肿。
  • 使用 docker commit命令生成的是黑箱镜像,除了制作镜像的作者清楚具体的改动,他人只能通过docker diff命令获取很少的线索,难以维护。

示例:

[root@CentOS-7 ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu        latest              6a2f32de169d        6 days ago          117.2 MB
[root@CentOS-7 ~]# 
[root@CentOS-7 ~]# docker run --name TestCommitImages -d -it ubuntu bash
ca9cb66a39ca0271149fadce05523d41488a5c047196de510fe54c0f26a46b25
[root@CentOS-7 ~]# 
[root@CentOS-7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ca9cb66a39ca        ubuntu              "bash"              4 seconds ago       Up 2 seconds                            TestCommitImages
[root@CentOS-7 ~]# 
[root@CentOS-7 ~]# docker exec -it TestCommitImages bash
root@ca9cb66a39ca:/# echo "this is a test !" >> testcommit.log
root@ca9cb66a39ca:/# ls -l testcommit.log 
-rw-r--r-- 1 root root 17 Apr 19 03:04 testcommit.log
root@ca9cb66a39ca:/# cat testcommit.log 
this is a test !
root@ca9cb66a39ca:/# exit
exit
[root@CentOS-7 ~]# 
[root@CentOS-7 ~]# docker diff TestCommitImages
C /run
A /run/secrets
C /root
A /root/.bash_history
A /testcommit.log
[root@CentOS-7 ~]# 
[root@CentOS-7 ~]# docker commit --author "anliven" --message "touch new file" TestCommitImages ubuntu:commit
sha256:87575bc0c80f7dd9230072626a31cdeea5c6163e70b559f5432eaac881bd2376
[root@CentOS-7 ~]# 
[root@CentOS-7 ~]# docker images ubuntu
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              commit              87575bc0c80f        36 seconds ago      117.2 MB
docker.io/ubuntu    latest              6a2f32de169d        6 days ago          117.2 MB
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# docker history ubuntu:commit
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
87575bc0c80f        9 minutes ago       bash                                            106 B               touch new file
6a2f32de169d        6 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0 B                 
<missing>           6 days ago          /bin/sh -c mkdir -p /run/systemd && echo 'doc   7 B                 
<missing>           6 days ago          /bin/sh -c sed -i 's/^#s*(deb.*universe)$/   2.759 kB            
<missing>           6 days ago          /bin/sh -c rm -rf /var/lib/apt/lists/*          0 B                 
<missing>           6 days ago          /bin/sh -c set -xe   && echo '#!/bin/sh' > /u   745 B               
<missing>           6 days ago          /bin/sh -c #(nop) ADD file:b8a2c16d65e533a2bc   117.2 MB            
[root@CentOS-7 ~]# 
[root@CentOS-7 ~]# docker run --name NewImages -it ubuntu:commit bash
root@cfa4b8cf7d2c:/# ls -l testcommit.log 
-rw-r--r-- 1 root root 17 Apr 19 03:04 testcommit.log
root@cfa4b8cf7d2c:/# cat testcommit.log 
this is a test !
root@cfa4b8cf7d2c:/# exit
exit
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
cfa4b8cf7d2c        ubuntu:commit       "bash"              2 hours ago         Exited (0) 6 seconds ago                       NewImages
ca9cb66a39ca        ubuntu              "bash"              2 hours ago         Up 2 hours                                     TestCommitImages
[root@CentOS-7 ~]# 
[root@CentOS-7 ~]# docker logs NewImages
root@cfa4b8cf7d2c:/# ls -l testcommit.log 
-rw-r--r-- 1 root root 17 Apr 19 03:04 testcommit.log
root@cfa4b8cf7d2c:/# cat testcommit.log 
this is a test !
root@cfa4b8cf7d2c:/# exit
exit
[root@CentOS-7 ~]# 
原文地址:https://www.cnblogs.com/anliven/p/6733353.html