docker commit :表示提交一个容器的副本使之成为新的镜像。假如我们在docker上运行了一个tomcat的容器,对Tomcat容器进行了修改操作,然后我们将修改操作后的tomcat进行commit,就得到了一个新的镜像。
命令格式:docker commit -m=“提交的描述信息” -a="作者" 容器id 要创建的目标镜像名:[标签名]
案例演示
1、从hub上下载tomcat镜像到本地并运行成功
说明: -p是指定端口号。-P是随机端口号
[root@t-docker chenzx]# docker images tomcat REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 690cb3b9c7d1 7 days ago 463MB [root@t-docker chenzx]# docker run -it -p 8888:8080 tomcat Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /docker-java-home/jre 30-Aug-2018 08:37:01.728 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] 30-Aug-2018 08:37:01.736 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"] 30-Aug-2018 08:37:01.739 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 555 ms [root@t-docker chenzx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5c83a1f3aea4 tomcat "catalina.sh run" 8 minutes ago Up 8 minutes 0.0.0.0:8888->8080/tcp quirky_zhukovsky
[root@t-docker chenzx]# docker run -it -P tomcat [root@t-docker chenzx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 54a817e3ce05 tomcat "catalina.sh run" 21 seconds ago Up 19 seconds 0.0.0.0:32768->8080/tcp confident_leavitt
2、故意删除上一步镜像产生的tomcat容器文档
[root@t-docker chenzx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 54a817e3ce05 tomcat "catalina.sh run" 17 minutes ago Up 17 minutes 0.0.0.0:32768->8080/tcp confident_leavitt [root@t-docker chenzx]# docker exec -it 54a817e3ce05 /bin/bash root@54a817e3ce05:/usr/local/tomcat# cd webapps/ root@54a817e3ce05:/usr/local/tomcat/webapps# ls -l total 4 drwxr-x--- 3 root root 306 Aug 23 00:47 ROOT drwxr-x--- 14 root root 4096 Aug 23 00:47 docs drwxr-x--- 6 root root 83 Aug 23 00:47 examples drwxr-x--- 5 root root 87 Aug 23 00:47 host-manager drwxr-x--- 5 root root 103 Aug 23 00:47 manager root@54a817e3ce05:/usr/local/tomcat/webapps# rm -rf docs/ root@54a817e3ce05:/usr/local/tomcat/webapps# ls -l total 0 drwxr-x--- 3 root root 306 Aug 23 00:47 ROOT drwxr-x--- 6 root root 83 Aug 23 00:47 examples drwxr-x--- 5 root root 87 Aug 23 00:47 host-manager drwxr-x--- 5 root root 103 Aug 23 00:47 manager
3、通过上一步,当前的tomcat是没有doc目录的容器,以他为模板commit一个没有doc的tomcat新镜像zhenzx/tomcat02,zhenxz就是自己取名的命名空间
[root@t-docker chenzx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 54a817e3ce05 tomcat "catalina.sh run" 26 minutes ago Up 26 minutes 0.0.0.0:32768->8080/tcp confident_leavitt [root@t-docker chenzx]# docker commit -a='xiaoxin' -m='del tomcat docs' 54a817e3ce05 chenzx/tomcat02:1.2 sha256:24c3d89d4d043e4fb97f0cf4f3b94bcf96b5600a062fc8de4e39a7763d20badf [root@t-docker chenzx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE chenzx/tomcat02 1.2 24c3d89d4d04 7 seconds ago 463MB tomcat latest 690cb3b9c7d1 7 days ago 463MB
把正在运行的容器全部删除
[root@t-docker chenzx]# docker rm -f $(docker ps -q) 54a817e3ce05 [root@t-docker chenzx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
以我们自定义的镜像生成一个新的容器
[root@t-docker chenzx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE chenzx/tomcat02 1.2 24c3d89d4d04 6 minutes ago 463MB tomcat latest 690cb3b9c7d1 7 days ago 463MB centos latest 5182e96772bf 3 weeks ago 200MB [root@t-docker chenzx]# [root@t-docker chenzx]# docker run -it -P chenzx/tomcat02:1.2 [root@t-docker chenzx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c6c68b224e84 chenzx/tomcat02:1.2 "catalina.sh run" 24 seconds ago Up 24 seconds 0.0.0.0:32770->8080/tcp awesome_sammet
连接后可以看到,我们用自定义的tomcat1.2镜像没有doc目录
6、启动我们新的镜像并和原来的镜像对比
启动chenzx/tomcat1.2,它没有doc目录
启动原来的tomcat,它有doc目录
[root@t-docker chenzx]# docker run -d -p 6666:8080 tomcat 23b4ec6f24b854f682631c5a9b6bcfc0c685dbec46d1867faea4b534f5791452 [root@t-docker chenzx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 23b4ec6f24b8 tomcat "catalina.sh run" 5 seconds ago Up 4 seconds 0.0.0.0:6666->8080/tcp flamboyant_almeida
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28916011/viewspace-2213229/,如需转载,请注明出处,否则将追究法律责任