使用docker commit基于容器制作镜像
1、格式:
[root@localhost ~]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image (default [])
--help Print usage
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
-a
,作者(例如,
“along
along@along.com
”
)
-c
,修改
Dockerfifile
指令应用于创建的镜像
-m
,提交消息
-p
,在提交期间暂停容器(默认为
true
)
示例一:简单的基于容器创建一个新的镜像
(1)先运行一个容器
[root@localhost ~]# docker run --name b1 -it busybox
/ # ls
bin dev etc home proc root sys tmp usr var
/ # mkdir -p /data/html
/ # echo "<h1>busybox httpd server</h1>" > /data/html/index.html
/ # cat /data/html/index.html
<h1>busybox httpd server</h1>
(2)不用退出这个容器,另起终端在b1容器基础上,制作新镜像
[root@localhost ~]# docker commit -p b1
sha256:34b70e5080a9d07ec9a87bf8316c1c5a492f9b8e344d1270cc92932352a11386
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 34b70e5080a9 11 seconds ago 1.24 MB
(3)给新绘制的镜像打标签
[root@localhost ~]# docker tag 34b70e5080a9 zjm/httpd:v0.1
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
zjm/httpd v0.1 34b70e5080a9 About a minute ago 1.24 MB
(4)可以对同一个镜像载再打标签
[root@localhost ~]# docker tag zjm/httpd:v0.1 ajm/httpd:v0.2
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ajm/httpd v0.2 34b70e5080a9 3 minutes ago 1.24 MB
zjm/httpd v0.1 34b70e5080a9 3 minutes ago 1.24 MB
(5)删除同一个镜像的标签,只是把这个标签去掉,只到删除这个镜像的最后一个标签,此镜像才会被删除
[root@localhost ~]# docker rmi ajm/httpd:v0.2
Untagged: ajm/httpd:v0.2
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
zjm/httpd v0.1 34b70e5080a9 5 minutes ago 1.24 MB
(6)基于新的镜像运行一个容器,验证是否是基于b1创建的
[root@localhost ~]# docker run --name b2 -it zjm/httpd:v0.1
/ # cat /data/html/index.html
<h1>busybox httpd server</h1>
示例二:基于容器创建新的镜像,并修改执行命令CMD
(1)基于容器b1创建新的镜像,并修改命令为执行httpd服务
[root@localhost ~]# docker commit -a "Along <along@along.com>" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p b1 along/httpd:v0.2
sha256:565ee1d9d7f3424d7f5ca803a14dbc727323c409962e39ebd0682afd1f51faa1
-f
:不运行为守护进程,在前台运行
-h
:指定
httpd
运行的主目录
(2)运行新的镜像v0.2
[root@localhost ~]# docker run --name b3 -d along/httpd:v0.2
f75797df94b1111b35d67b0316b4e35e86bbe7ae04f9f23c162a78ba4b5bf45f
(3)验证成功
[root@localhost ~]# docker inspect b3 |grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",