去 Docker Hub 搜镜像的版本信息

拉取镜像

  1. [root@centos01 ~]# docker pull tomcat:9.0
  2. 9.0: Pulling from library/tomcat
  3. d960726af2be: Pull complete
  4. e8d62473a22d: Pull complete
  5. 8962bc0fad55: Pull complete
  6. 65d943ee54c1: Pull complete
  7. da20b77f10ac: Pull complete
  8. 8669a096f083: Pull complete
  9. e0c0a5e9ce88: Pull complete
  10. f7f46169d747: Pull complete
  11. 42d8171e56e6: Pull complete
  12. 774078a3f8bb: Pull complete
  13. Digest: sha256:71703331e3e7f8581f2a8206a612dbeedfbc7bb8caeee972eadca1cc4a72e6b1
  14. Status: Downloaded newer image for tomcat:9.0
  15. docker.io/library/tomcat:9.0
  16. # 查看是否拉取成功
  17. [root@centos01 ~]# docker images
  18. REPOSITORY TAG IMAGE ID CREATED SIZE
  19. tomcat 9.0 c43a65faae57 2 weeks ago 667MB
  20. mysql 5.7 2c9028880e58 2 weeks ago 447MB

创建并运行容器

# 进行端口映射
[root@centos01 ~]# docker run -d --name tomcat01 -p 8080:8080 c43a65faae57
87256ea40bb861e2dbf029cd8450be955cb042df2a4609abab17a27c9ed2fef2

# 查看时候启动成功
[root@centos01 ~]# docker ps
CONTAINER ID   IMAGE          COMMAND             CREATED         STATUS         PORTS                                       NAMES
87256ea40bb8   c43a65faae57   "catalina.sh run"   8 seconds ago   Up 7 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   tomcat01
[root@centos01 ~]#

外部访问容器内的 tomcat

image.png
提示未找到,我们进入容器查看以下原因

进入容器

[root@centos01 ~]# docker exec -it tomcat01 /bin/bash
root@87256ea40bb8:/usr/local/tomcat#

进入容器,发现 /webapps 目录下没有内容。但是有一个 /webapps 目录,里面有 ROOT 项目。
将 ROOT 项目复制到 /webapps 目录下,在进行访问。

root@87256ea40bb8:/usr/local/tomcat# cp -r  webapps.dist/ROOT webapps

外部访问容器内的 tomcat

image.png
可以访问成功。

每次都进入容器有点麻烦,可以设置改在目录,将容器的指定目录挂载到主机的指定目录,这样就可以直接在主机上进行操作了。