前言
我们一直在使用别人打好的镜像来使用,如果需要添加我们自己的东西该怎么做呢,接下来介绍。
Docker镜像commit操作案例
更改容器
默认的ubuntu镜像没有vim、ifconfig命令,这里我们先从ubuntu镜像启动一个容器,在容器里安装vim和ifconfig命令,然后将这个容器利用commit重新打包镜像。
[root@localhost ~]# docker run -it ubuntu # 这里没有加-d,只用-it表示启动容器后直接进入容器内部终端
root@812b2a087248:/# apt-get update
root@812b2a087248:/# apt-get install -y vim # 安装vim命令
root@812b2a087248:/# apt-get install -y net-tools # 安装ifconfig命令
# 安装完成,退出容器(两种方式):
# Ctrl+p+q 退出容器,容器依然后台运行
# exit 退出容器,容器同时关闭
使用commit将容器打包为新的镜像
语法:
docker commit -m="描述信息" -a="作者" 容器ID 要创建的目标镜像名:[标签名]
操作示例:
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
812b2a087248 ubuntu "bash" 11 minutes ago Up 11 minutes vigilant_mestorf
[root@localhost ~]#
[root@localhost ~]# docker commit -m="add vim command" -a="Alex" 812b2a087248 alex/ubuntu:v1.0
sha256:80e71251231cb6c28ccf59137cdf4ac097c1356371faa25d78719ee94d51cde1
[root@localhost ~]#
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alex/ubuntu v1.0 80e71251231c 7 seconds ago 175MB
ubuntu latest ba6acccedd29 4 months ago 72.8MB
成功创建的一个新的镜像 alex/ubuntu:v1.0
验证
[root@localhost ~]# docker run -it alex/ubuntu:v1.0
root@077ecca02072:/# vim
vim vim.basic vimdiff vimtutor
root@077ecca02072:/# whereis vim
vim: /usr/bin/vim /usr/bin/vim.basic /etc/vim /usr/share/vim
root@077ecca02072:/#
root@077ecca02072:/#
成功。