Docker - 构建镜像:使用docker commit命令

使用docker commit命令生成镜像

Docker镜像是多层存储,每一层是在前一层的基础上进行的修改,而容器可以理解为是以镜像为基础层的多层存储。
如果不使用数据卷,运行一个容器的时候,对任何文件的修改都会被记录于容器存储层。

一般情况下,很少真正从头开始创建一个全新的镜像,大多是基于一个已有的基础镜像,例如Ubuntu和CentOS等,来构建出新的镜像。
docker commit命令可以将容器存储层保存下来成为镜像,也就是将原有镜像的基础层和容器存储层并构成包含原有容器最后文件变化的新镜像。
docker commit命令可以简单理解为“向镜像的版本控制系统里提交变更”。

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

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

示例-1

[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 ~]# 

示例-2

[root@anliven ~]# docker run -i -t ubuntu /bin/bash
root@5e98a9aed357:/# apt-get -y update
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [107 kB]
Get:3 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [1078 B]
Get:4 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [231 kB]
Get:5 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [67.5 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [45.4 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal-updates InRelease [111 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]
Get:9 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
Get:10 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [17.3 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [45.5 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [454 kB]
Get:16 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [207 kB]
Get:17 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [3195 B]
Fetched 14.5 MB in 1min 28s (164 kB/s)
Reading package lists... Done
root@5e98a9aed357:/#
root@5e98a9aed357:/# apt-get install -y apache2
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
......
......
......
Processing triggers for libc-bin (2.31-0ubuntu9) ...
Processing triggers for ca-certificates (20190110ubuntu1.1) ...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
root@5e98a9aed357:/#
root@5e98a9aed357:/# exit
exit
[root@anliven ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
5e98a9aed357        ubuntu              "/bin/bash"         8 minutes ago       Exited (0) 21 seconds ago                       pedantic_hodgkin
b07a76e1cfc9        hello-world         "/hello"            3 months ago        Exited (0) 3 months ago                         vigorous_williams
[root@anliven ~]#
[root@anliven ~]# docker commit 5e98a9aed357 demo/webserver
sha256:784f50ab9fb48ff33ade0884acfb784c565a89a387720e42f65a4c139413ffc1
[root@anliven ~]#
[root@anliven ~]# docker images |grep webserver
demo/webserver        latest              784f50ab9fb4        17 seconds ago      210MB
[root@anliven ~]#
posted @ 2017-04-19 14:31  Anliven  阅读(1875)  评论(0编辑  收藏  举报