镜像分层理解实践&docker commit 实操

镜像分层理解实践&docker commit 实操

  • [1 docker commit 命令语法](#1 docker commit 命令语法)
  • [2 实操目标](#2 实操目标)

1 docker commit 命令语法

docker commit命令用于 从运行中的容器提交为镜像,通常是在容器中编辑文件或者扩展新功能后 保存为新镜像

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Options:

-a, --author string Author (e.g., "John Hannibal Smith hannibal@a-team.com")

-c, --change list Apply Dockerfile instruction to the created image

-m, --message string Commit message

-p, --pause Pause container during commit (default true)
常用形式

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

2 实操目标

我们在ubuntu 容器总增加vim 命令后,保存为新镜像,并使用新镜像启动一个新容器

  1. 拉取镜像
bash 复制代码
#拉取ubuntu 镜像
$ docker pull ubuntu:latest
#查看本地镜像
$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       latest    ba6acccedd29   2 years ago   72.8MB
  1. 启动一个ubuntu 容器
bash 复制代码
#启动容器
$ docker run -it --name=ubuntu1 ubuntu /bin/bash
#容器内执行vim 命令 发现没有
root@a077efb7baf7:/$ vim
bash: vim: command not found
  1. 容器内安装vim
bash 复制代码
root@a077efb7baf7:/$ apt-get update
root@a077efb7baf7:/$ apt-get install vim
#创建一个b.txt 文件
root@a077efb7baf7:/tmp$ touch b.txt && echo "124" > b.txt
root@a077efb7baf7:/tmp$ vim b.txt
#确认已经安装好了vim
  1. 提交镜像
bash 复制代码
#在主机上提交镜像
$ docker commit -m="ubuntu add vim" -a="my" a077 myubuntu:vim
sha256:c0ef6bd37906e4f86b149a9296669cc4cd103018d512cf11e5456d40469734f2
$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
myubuntu     vim       c0ef6bd37906   6 seconds ago   189MB
ubuntu       latest    ba6acccedd29   2 years ago     72.8MB
#已经有新镜像myubuntu并且新镜像要比原来的镜像大很多
  1. 运行新的容器
bash 复制代码
#运行新的容器
$ docker run -it --name=myubuntu1  myubuntu:vim  /bin/bash
#查看提交镜像前创建的文件 ,发现还存在
$ cd /tmp
$ cat b.txt 
124
#执行vim命令 确定存在vim
root@b01f01f7d267:/tmp$ vim b.txt
相关推荐
liliangcsdn1 小时前
Mac本地docker安装Kibana+ElasticSearch
elasticsearch·macos·docker
恒创科技HK1 小时前
现在中国香港服务器速度怎么样?
运维·服务器
创业之路&下一个五年1 小时前
第一部分:服务器硬件配置
运维·服务器
xiao-xiang2 小时前
k8s下的发布策略详解
云原生·容器·kubernetes·部署·cicd·发布
优秀的老黄2 小时前
Docker部署RabbitMQ
linux·运维·docker·中间件·容器·centos·rabbitmq
Lin_Aries_04212 小时前
容器使用卷
linux·运维·docker·云原生·容器·eureka
Liang_GaRy3 小时前
心路历程-Linux的特殊权限
linux·运维·服务器
非凡ghost3 小时前
AOMEI Partition Assistant磁盘分区工具:磁盘管理的得力助手
linux·运维·前端·数据库·学习·生活·软件需求
山君爱摸鱼3 小时前
Linux网络配置
linux·运维