拉取镜像
docker pull tomcat:9.0
启动Tomcat
[root@izj6cev682kqg86i4ogj8rz ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 months ago 141MB
tomcat 9.0 b8e65a4d736d 5 months ago 680MB
tomcat latest fb5657adc892 5 months ago 680MB
mysql latest 3218b38490ce 5 months ago 516MB
hello-world latest feb5d9fea6a5 8 months ago 13.3kB
centos latest 5d0da3dc9764 8 months ago 231MB
[root@izj6cev682kqg86i4ogj8rz ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
08e8a0cbc58e centos "/bin/bash" 4 hours ago Up 4 hours gracious_chaum
[root@izj6cev682kqg86i4ogj8rz ~]# docker run -d -p 3355:8080 --name tomcat02 tomcat
4a76a3c72b2378231dbd38371006112b9f48a9d43dfea0c79e38be06f5debe27
[root@izj6cev682kqg86i4ogj8rz ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a76a3c72b23 tomcat "catalina.sh run" 13 seconds ago Up 12 seconds 0.0.0.0:3355->8080/tcp tomcat02
08e8a0cbc58e centos "/bin/bash" 4 hours ago Up 4 hours gracious_chaum
[root@izj6cev682kqg86i4ogj8rz ~]#
参数意义:
- -d:后台运行
- -p:端口暴露映射
- —name:给容器取一个名字
:::danger
注意:
这时候访问服务器的3355端口,会出现404错误,但是tomcat部署上去了。
进入容器会发现linux命令少了,没有webapps。这是阿里云镜像的原因,默认是最小镜像,所有不必要的都要剔除掉,保证最小可以运行的环境。
:::
进入容器修改让Tomcat初始页面显示出来
[root@izj6cev682kqg86i4ogj8rz ~]# docker exec -it tomcat02 /bin/bash
root@4a76a3c72b23:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist
CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
root@4a76a3c72b23:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@4a76a3c72b23:/usr/local/tomcat# cd webapps
root@4a76a3c72b23:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
root@4a76a3c72b23:/usr/local/tomcat/webapps#