>
docker commit 命令 : 用于將一個鏡像進行提交成一個新的副本
命令格式:
docker commit -m="描述信息" -a="作者" 容器id 目標鏡像名: [TAG]
做個實例:
查看本地鏡像
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 2 months ago 141MB tomcat 9.0 b8e65a4d736d 2 months ago 680MB tomcat latest fb5657adc892 2 months ago 680MB mysql latest 3218b38490ce 2 months ago 516MB hello-world latest feb5d9fea6a5 5 months ago 13.3kB centos latest 5d0da3dc9764 5 months ago 231MB
我們之前的Tomcat服務器的部署是使用的最新的latest
我們就使用Tomcat 9.0 來做這個
這是它的前臺運行結果
做一個端口映射:
[root@localhost ~]# docker run -it -p 8080:8080 tomcat Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/openjdk-11 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Using CATALINA_OPTS: NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED 07-Mar-2022 10:59:56.387 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/10.0.14 07-Mar-2022 10:59:56.390 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Dec 2 2021 22:01:36 UTC 07-Mar-2022 10:59:56.390 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 10.0.14.0 07-Mar-2022 10:59:56.391 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux 07-Mar-2022 10:59:56.392 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-862.el7.x86_64 07-Mar-2022 10:59:56.392 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64 07-Mar-2022 10:59:56.392 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/local/openjdk-11 07-Mar-2022 10:59:56.392 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 11.0.13+8
在前臺運行的同時,我們來查看一下正在運行的容器
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5e73948b0078 tomcat "catalina.sh run" 5 minutes ago Up 5 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp happy_feistel 155ead9f80cc tomcat "catalina.sh run" 5 hours ago Up 5 hours 0.0.0.0:3355->8080/tcp, :::3355->8080/tcp tomcat01 2257878e8b61 nginx "/docker-entrypoint.…" 6 hours ago Up 5 hours 0.0.0.0:443->80/tcp, :::443->80/tcp nginx01 6d6fa6a4c6d7 5d0da3dc9764 "/bin/bash" 6 hours ago Up 6 hours nice_panini [root@localhost ~]#
這個時候,使用瀏覽器來訪問一下Tomcat
這結果是正常的,這是因為鏡像原本就不完整
在這樣的情況下,如果終止掉前臺的進程
這個時候,瀏覽器的訪問就會報錯:
如果想要在終止前臺進程的同時,想讓瀏覽器的訪問不報錯
就要這樣做:
[root@localhost ~]# docker run -d -p 8080:8080 --name tomcat04 tomcat:9.0 c9a2f98234c79d94e0ccc67b5a529eace1ec1d2211b1b7088a16a45c62134ae8 [root@localhost ~]#
瀏覽器再次訪問
恢復正常
我們接下來解決一下鏡像不完整而導致的問題
進容器
[root@localhost ~]# docker exec -it b7d37ccaadf0 /bin/bash root@b7d37ccaadf0:/usr/local/tomcat# b7d37ccaadf0
這樣復制一下文件到指定文件中
root@c9a2f98234c7:/usr/local/tomcat# cp -r webapps.dist/* webapps root@c9a2f98234c7:/usr/local/tomcat# cd webapps root@c9a2f98234c7:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager root@c9a2f98234c7:/usr/local/tomcat/webapps#
瀏覽器上測試:
這就好了
接下來,我們要進行一些操作
使得我們修改后的鏡像提交成一個新的副本
下次我們直接使用它
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c9a2f98234c7 tomcat:9.0 "catalina.sh run" 17 minutes ago Up 17 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat04 155ead9f80cc tomcat "catalina.sh run" 6 hours ago Up 6 hours 0.0.0.0:3355->8080/tcp, :::3355->8080/tcp tomcat01 2257878e8b61 nginx "/docker-entrypoint.…" 6 hours ago Up 6 hours 0.0.0.0:443->80/tcp, :::443->80/tcp nginx01 6d6fa6a4c6d7 5d0da3dc9764 "/bin/bash" 7 hours ago Up 7 hours nice_panini
將我們原先經過處理的鏡像打成一個新的包,變成一個新的整體
[root@localhost ~]# docker commit -a="shuaige" -m "add a web app" c9a2f98234c7 tomcat04:1.0 sha256:12b32b1a124d5c1a3572fd0d02542c254192e415aa49045390f15a9a811f0153 [root@localhost ~]#
查看鏡像:(看Tomcat04 的那個鏡像,比原先的9.0 Tomcat 鏡像要大)
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat04 1.0 12b32b1a124d 2 minutes ago 685MB nginx latest 605c77e624dd 2 months ago 141MB tomcat 9.0 b8e65a4d736d 2 months ago 680MB tomcat latest fb5657adc892 2 months ago 680MB mysql latest 3218b38490ce 2 months ago 516MB hello-world latest feb5d9fea6a5 5 months ago 13.3kB centos latest 5d0da3dc9764 5 months ago 231MB
現在他就是一個新的鏡像了,下次可以直接使用
>