5.容器与镜像
1 Docker 容器与镜像的关系

2 容器提交 - docker container commit
-
作用:
根据容器的更改创建新镜像 -
命令格式:
docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] -
命令参数:
参数 默认值 描述 -a, --author作者 -c, --change将 Dockerfile 指令应用于创建的镜像 -m, --message提交消息,类似 git commit -m-p, --pausetrue 提交期间暂停容器
2.1 容器提交
在 centos8 容器中安装 net-tools 工具(修改 centos8 安装源),并提交镜像
zhy@ubuntu24-04:~$ docker container exec -ti af40 bash
[root@af400260292c /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 18573 bytes 95781458 (91.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 17343 bytes 1008051 (984.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@af400260292c /]# exit
exit
zhy@ubuntu24-04:~$ docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af400260292c centos "bash" 18 minutes ago Up 18 minutes great_euclid
zhy@ubuntu24-04:~$ docker container commit -m "install net-tools" af40 centos-net:v1.0
sha256:0889019b63f1d00bf9d5696f9f9a422ec59129061897eb753cfd65b3ba9f648d
zhy@ubuntu24-04:~$ docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af400260292c centos "bash" 20 minutes ago Up 20 minutes great_euclid
zhy@ubuntu24-04:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-net v1.0 0889019b63f1 14 seconds ago 534MB
nginx latest 66f8bdd3810c 4 weeks ago 192MB
centos latest 5d0da3dc9764 3 years ago 231MB
zhy@ubuntu24-04:~$
zhy@ubuntu24-04:~$ docker container run -ti 0889 bash
[root@22a9e263904b /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 16 bytes 2098 (2.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@22a9e263904b /]# exit
exit
zhy@ubuntu24-04:~$
3 容器导出 - docker container export
-
作用:
将容器的文件系统导出为 tar 存档 -
命令格式:
docker container export [OPTIONS] CONTAINER -
命令参数:
参数 默认值 描述 -o, --output写入文件,而不是 STDOUT
3.1 容器导出
将安装了 net-tools 工具的 centos 镜像导出为 net-tools-image 镜像

4 镜像导入 - docker image import
-
作用:
从 tar 导入内容以创建文件系统映像 -
命令格式:
docker image import [OPTIONS] file|URL|- [REPOSITORY[:TAG]] -
命令参数:
参数 默认值 描述 -c, --change将 Dockerfile 指令应用于创建的镜像 -m, --message为导入的图像设置提交消息
4.1 镜像导入
将 net-tools-image tar 文件导入创建镜像

5 commit 和 import 对比
-
从下图可以看出,commit 方式生成的镜像保留了历史分层信息,而 import 方式则将所有层合到一起生成了全新的一层镜像
![images/5.容器与镜像/Pasted-image-20241229134447.png]()
-
docker inspect查看两种方式生成的镜像的 Config 信息,即 元数据,可以发现 commit 方式提交的镜像保留了所有元数据,而 import 方式提交的则丢失了commit 方式提交的
"Config": { "Hostname": "af400260292c", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": true, "OpenStdin": true, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "bash" ], "Image": "centos", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20210915", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS" } },import 方式提交的
"Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": null, "Cmd": null, "Image": "", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null },
6 联合文件系统和 Docker 镜像
详细介绍
docker 镜像是分层的,每一层都有各自的文件系统,且每层的文件系统是只读的。

Docker 加载镜像会一次同时加载多个文件系统,但从外面看,只能看到一个文件系统,联合加载把各层文件系统提升起来,这样最终的文件系统会包含所有底层的文件和目录。如果对镜像的操作改变了顶层镜像的文件系统,那么就会在最顶层上新建一层镜像。

7 联合文件系统与容器
容器实际就是在镜像上新添加一个可读可写的文件系统,如果操作改变了文件系统,那么 commit 就会将该可读可写层转换成为一个只读文件系统,来保留对文件系统的改变。如果操作没有改变可读可写层,那么就不会将该层保留下来

容器的运行

容器的视角

容器与镜像的底层关系




浙公网安备 33010602011771号