commit Create a new image from a container's changes
commit 从容器的变化中构建出一个新的镜像
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
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
简单翻译一下:
从容器的更改创建新映像
选项:
-a, --author string 作者(例如,“John Hannibal Smith hannibal@a-team.com”)
-c, --change list 应用dockerfile指令来创建图像
-m, --message string 提交信息
-p, --pause 提交期间暂停容器(默认为true)
当容器中的文件产生变化后,可以提交生成一个新的image,这可以让你debug出你所做的修改,导出发布到其他服务器,这似乎是一种比Dockerfile更好的管理方式。
[root@localhost dockerfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f61fe8c1b847 nginx:1 "/bin/bash" About an hour ago Up About an hour 0.0.0.0:8080->80/tcp epic_ride
cf2c588a7b30 centos "/bin/bash" 3 days ago Up 3 days nifty_yonath
[root@localhost dockerfile]# docker attach cf2c588a7b30
(备注:这个容器我在上一篇文章里为了验证nginx的安装命令,有安装过nginx,所以在这里我直接修改Index.html再启动nginx然后打包镜像)
[root@cf2c588a7b30 /]# vi /usr/share/nginx/html/index.html
hello docker
[root@cf2c588a7b30 /]# /usr/sbin/nginx
CTRL P Q
[root@cf2c588a7b30 /]# read escape sequence
[root@localhost dockerfile]# docker commit -a "by hyhy" -m "add nginx" -p cf2c588a7b30 centos_nginx:new
sha256:bc91ce331137f1df4fe68c5a01e0acb98fe10eaad4c57a19a090fa5b880467d5
[root@localhost dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx new bc91ce331137 6 seconds ago 384MB
nginx 1 f31a08eb3bee 2 hours ago 286MB
centos latest 2d194b392dd1 6 days ago 195MB
busybox latest f6e427c148a7 11 days ago 1.15MB
mysql latest 5d4d51c57ea8 13 days ago 374MB
registry.docker-cn.com/library/nginx latest e548f1a579cf 2 weeks ago 109MB
hello-world latest f2a91732366c 3 months ago 1.85kB
[root@localhost dockerfile]# docker run -it bc91ce331137 /bin/bash
[root@fd98b5e5a07b /]# curl 127.0.0.1
curl: (7) Failed connect to 127.0.0.1:80; Connection refused
[root@fd98b5e5a07b /]# /usr/sbin/nginx
[root@fd98b5e
docker commit的修改是在shell中的,而shell中是没有启动任何服务的,如果你希望制作的镜像中的服务是随容器启动而启动的话,需要制作启动脚本
[root@localhost dockerfile]# docker attach cf2c588a7b30
[root@a75dfeff0898 /]# vi /nginx.sh
#!/bin/sh
/usr/sbin/nginx
/bin/bash
[root@cf2c588a7b30 /]# chmod 777 nginx.sh
[root@cf2c588a7b30 /]# read escape sequence
[root@localhost dockerfile]# docker commit --pause=false cf2c588a7b30 centos_nginx:1
sha256:f982716615b28f2e7ce9b565ff3aa2da421c5bb578c03934a66f92474f8d6aa0
[root@localhost dockerfile]# docker run -it centos_nginx:1 /nginx.sh
[root@a75dfeff0898 /]# curl 127.0.0.1
hello docker
这样就实现了容器中的服务随容器启动而启动了。