Docker 之commit使用

柳三千

文章最后更新时间:2025年06月12日

任务要求:

官方 centos 容器镜像没有 vim、ifconfig 命令,需生成一个自带 vim 命令的镜像,避免后续额外下载。

实行方案:

在 centos7.8 容器中安装 vim,通过 docker commit 提交容器副本生成新镜像。

[root@localhost docker]# docker images
REPOSITORY                  TAG       IMAGE ID       CREATED         SIZE
nginx                       latest    51086ed63d8c   3 weeks ago     142MB
ubuntu                      latest    216c552ea5ba   3 weeks ago     77.8MB
centos                      latest    5d0da3dc9764   13 months ago   231MB
martonyang/centos7.8.2003   latest    0c0f2dcf7afd   14 months ago   268MB

[root@localhost docker]# docker run -it 0c0f2dcf7afd bash
[root@a5b8d9c4e554 /]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)

[root@a5b8d9c4e554 /]# vim 1
bash: vim: command not found

[root@a5b8d9c4e554 /]# yum install vim -y

[root@a5b8d9c4e554 /]# vim 1.txt
[root@a5b8d9c4e554 /]# cat 1.txt 
install vim!
[root@a5b8d9c4e554 /]#

镜像提交命令

docker commit -m="提交的描述信息" -a="作者" 容器ID 镜像名:[标签名]

[root@localhost ~]# docker commit -m="add vim" -a="ldx" a5b8d9c4e554 centos_vim:7.8
sha256:e619078d3432ea1f5ccade7aa569b9f1572e678cd8342984212b542b5e94b5b6

# 镜像大小对比(安装vim后镜像翻倍,需慎用)
[root@localhost ~]# docker images
REPOSITORY                  TAG       IMAGE ID       CREATED         SIZE
centos_vim                  7.8       e619078d3432   6 seconds ago   546MB
nginx                       latest    51086ed63d8c   3 weeks ago     142MB
ubuntu                      latest    216c552ea5ba   3 weeks ago     77.8MB
centos                      latest    5d0da3dc9764   13 months ago   231MB
martonyang/centos7.8.2003   latest    0c0f2dcf7afd   14 months ago   268MB

最终使用新镜像测试:

# 未指定标签时默认查找 latest(失败案例)
[root@localhost ~]# docker run -it centos_vim bash
Unable to find image 'centos_vim:latest' locally


# 使用镜像 ID 启动
[root@localhost ~]# docker run -it e619078d3432 bash
[root@01878737a490 /]# exit
exit

# 使用完整镜像名:标签启动
[root@localhost ~]# docker run -it centos_vim:7.8 bash
[root@6e631459c2f0 /]# vim 123
[root@6e631459c2f0 /]# cat 1.txt 
install vim!
[root@6e631459c2f0 /]#
文章版权声明:除非注明,否则均为柳三千运维录原创文章,转载或复制请以超链接形式并注明出处。

取消
微信二维码
微信二维码
支付宝二维码