拉取镜像

  1. docker pull tomcat:9.0

启动Tomcat

  1. [root@izj6cev682kqg86i4ogj8rz ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx latest 605c77e624dd 5 months ago 141MB
  4. tomcat 9.0 b8e65a4d736d 5 months ago 680MB
  5. tomcat latest fb5657adc892 5 months ago 680MB
  6. mysql latest 3218b38490ce 5 months ago 516MB
  7. hello-world latest feb5d9fea6a5 8 months ago 13.3kB
  8. centos latest 5d0da3dc9764 8 months ago 231MB
  9. [root@izj6cev682kqg86i4ogj8rz ~]# docker ps
  10. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  11. 08e8a0cbc58e centos "/bin/bash" 4 hours ago Up 4 hours gracious_chaum
  12. [root@izj6cev682kqg86i4ogj8rz ~]# docker run -d -p 3355:8080 --name tomcat02 tomcat
  13. 4a76a3c72b2378231dbd38371006112b9f48a9d43dfea0c79e38be06f5debe27
  14. [root@izj6cev682kqg86i4ogj8rz ~]# docker ps
  15. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  16. 4a76a3c72b23 tomcat "catalina.sh run" 13 seconds ago Up 12 seconds 0.0.0.0:3355->8080/tcp tomcat02
  17. 08e8a0cbc58e centos "/bin/bash" 4 hours ago Up 4 hours gracious_chaum
  18. [root@izj6cev682kqg86i4ogj8rz ~]#

参数意义:

  • -d:后台运行
  • -p:端口暴露映射
  • —name:给容器取一个名字

:::danger 注意:
这时候访问服务器的3355端口,会出现404错误,但是tomcat部署上去了。
进入容器会发现linux命令少了,没有webapps。这是阿里云镜像的原因,默认是最小镜像,所有不必要的都要剔除掉,保证最小可以运行的环境。 :::

进入容器修改让Tomcat初始页面显示出来

  1. [root@izj6cev682kqg86i4ogj8rz ~]# docker exec -it tomcat02 /bin/bash
  2. root@4a76a3c72b23:/usr/local/tomcat# ls
  3. BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist
  4. CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
  5. root@4a76a3c72b23:/usr/local/tomcat# cp -r webapps.dist/* webapps
  6. root@4a76a3c72b23:/usr/local/tomcat# cd webapps
  7. root@4a76a3c72b23:/usr/local/tomcat/webapps# ls
  8. ROOT docs examples host-manager manager
  9. root@4a76a3c72b23:/usr/local/tomcat/webapps#