格式:
docker commit -a="作者" -m="提交的描述信息" 容器id 要创建的目标镜像名:[标签名]
docker run [options]
参数:
i
:交互
t
:终端
-p
: 主机端口:docker容器端口
-P
: 随要分配端口
常用的4各种格式:
ip:hostPort:containerPort
ip::containerPort
hostPort:containerPort
containerPort
格式:
docker run -it -p 宿主机端口:容器端口 tomcat
启动tomcat容器,并指定主机端口为8888 映射 tomcat端口为8080 的示例:
docker run -it -p 8888:8080 tomcat
root@localhost java]# docker images tomcat
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest ca9e2fccef98 3 weeks ago 463MB
[root@localhost java]# 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
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
25-Nov-2018 01:15:16.602 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.34
......(省略)......
25-Nov-2018 01:15:18.186 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [19] ms
25-Nov-2018 01:15:18.194 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
25-Nov-2018 01:15:18.279 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
25-Nov-2018 01:15:18.283 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1402 ms
测试
http://localhost:8888/
格式:
docker run -it -P tomcat
启动容器,端口号随机分配(tomcat的默认端口为8080,所有容器端为不变)
执行下面的命令前要确保端口号不被占用,特别是使用的tomcat的默认端口号8080
。
终端1:
[root@localhost java]# docker run -it -P 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
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
25-Nov-2018 01:21:47.210 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.34
25-Nov-2018 01:21:47.216 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Sep 4 2018 22:28:22 UTC
......(省略)......
25-Nov-2018 01:21:48.457 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [45] ms
25-Nov-2018 01:21:48.460 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
25-Nov-2018 01:21:48.481 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
25-Nov-2018 01:21:48.485 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1068 ms
在终端2,查看端口号
重新打开一个终端2
[root@localhost java]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d6c4e4d2471 tomcat "catalina.sh run" 17 seconds ago Up 15 seconds 0.0.0.0:32768->8080/tcp peaceful_pare
e5c067f6cf05 centos "/bin/bash" 7 hours ago Up 6 hours boring_yonath
[root@localhost java]#
从上面的PORTS
列中得得端口号是 32768 。
测试:
http://localhost:32768/
docker run -it -p 8888:8080 tomcat
和 docker run -it -P tomcat
命令都是前台执行的,一旦扫 ctrl + c
键后,则tomcat运行会终止。
目的: 对 方式一 启动的tomcat容器进行修改,删除docs文档及目录。
打开一个新终端 —— 终端3:
1、验证方式一启动的容器还有运行中
[root@localhost java]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01222e0b644b tomcat "catalina.sh run" 40 seconds ago Up 39 seconds 0.0.0.0:8888->8080/tcp quirky_bartik
[root@localhost java]#
2、删除docs 目录
在终端3下执行 docker exec -it 01222e0b644b /bin/sh
进入容器,并删除docs。
[root@localhost java]#
[root@localhost java]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01222e0b644b tomcat "catalina.sh run" 40 seconds ago Up 39 seconds 0.0.0.0:8888->8080/tcp quirky_bartik
[root@localhost java]# docker exec -it 01222e0b644b /bin/sh
# pwd
/usr/local/tomcat
# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf lib native-jni-lib webapps
CONTRIBUTING.md NOTICE RELEASE-NOTES bin include logs temp work
# cd webapps
# ls
ROOT docs examples host-manager manager
# rm -rf docs
# ls
ROOT examples host-manager manager
#
验证docs是否删除成功
点击:Docmentation
以当前的 tomcat 运行实例为模板,commit 一个没有 docs目录 的 tomcat 新镜像atguigu/tomcat1.2
格式:
docker commit -m="提交的描述信息" -a="作者" 容器id 要创建的目标镜像名:[标签名]
示例:
docker commit -a="xiaojin" -m="tomcat without docs" 01222e0b644b atguigu/mytomcat:1.2
打开新的终端3,执行命令:
[root@localhost java]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01222e0b644b tomcat "catalina.sh run" 20 minutes ago Up 20 minutes 0.0.0.0:8888->8080/tcp quirky_bartik
[root@localhost java]
[root@localhost java]
[root@localhost java]# docker commit -a="xiaojin" -m="tomcat without docs" 01222e0b644b atguigu/mytomcat:1.2
sha256:b105c1f52eb52c57fd6dbd86ae07b98adef98cd3c108fa99afe8f5db3f1ee358
查看本地的镜像:
[root@localhost java]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
atguigu/mytomcat 1.2 b105c1f52eb5 22 seconds ago 463MB
redis latest c188f257942c 8 days ago 94.9MB
tomcat latest ca9e2fccef98 3 weeks ago 463MB
centos latest 75835a67d134 6 weeks ago 200MB
[root@localhost java]#
以新的镜像 atguigu/mytomcat:1.2
为模板,启动新的容器。
指定端口号,并启动容器: docker run -it -p 7777:8080 atguigu/mytomcat:1.2
[root@localhost java]# docker run -it -p 7777:8080 atguigu/mytomcat:1.2
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
......(省略)......
25-Nov-2018 02:04:27.730 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [40] ms
25-Nov-2018 02:04:27.746 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
25-Nov-2018 02:04:27.767 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
25-Nov-2018 02:04:27.803 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 849 ms
打开浏览器,输入http://localhost:7777/
,
点击:Docmentation
docker run -d
后台运行镜像[root@localhost java]# docker run -d -p 6666:8080 tomcat
f2a576d5763b37d315b99968f3c829ec8e04bedcea835d6a2c5c91e1e51b8724
[root@localhost java]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f2a576d5763b tomcat "catalina.sh run" 6 seconds ago Up 4 seconds 0.0.0.0:6666->8080/tcp eloquent_chebyshev
8e705ac41b09 atguigu/mytomcat:1.2 "catalina.sh run" 36 minutes ago Up 36 minutes 0.0.0.0:7777->8080/tcp dazzling_meninsky
[root@localhost java]#
测试: http://localhost:6666/