大致流程是一致的,建议先在仓库里面搜索,每一个镜像都要详细的介绍和使用教程;

Docker部署Tomcat:

# 第一步:搜索镜像(可省略)
docker search tomcat

# 第二步:下载镜像
docker pull tomcat

# 第三步:运行
[root@zibo /]# docker images # 已下载的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 3 weeks ago 13.3kB
tomcat latest 040bdb29ab37 2 months ago 649MB
nginx latest f6d0b4767a6c 2 months ago 133MB
centos latest 300e315adb2f 3 months ago 209MB
[root@zibo /]# docker ps # 正在运行中的镜像
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6501c7f2c10e nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:3344->80/tcp nginx01
[root@zibo /]# docker stop 6501c7f2c10e # 把它停了,我要用3344端口测试tomcat
6501c7f2c10e
[root@zibo /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@zibo /]# docker run -d --name tomcat01 -p 3344:8080 tomcat # 运行tomcat
05b7fe07b68d4128848945eb1c4b137683e554b771b185747fc43e89c55dad9a
[root@zibo /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05b7fe07b68d tomcat "catalina.sh run" 6 seconds ago Up 5 seconds 0.0.0.0:3344->8080/tcp tomcat01

# 第四步:测试访问,可访问到,但发现tomcat是被阉割的

# 第五步:进入容器
docker exec -it 05b7fe07b68d /bin/bash

# 控制台
[root@zibo /]# docker exec -it 05b7fe07b68d /bin/bash
root@05b7fe07b68d:/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@05b7fe07b68d:/usr/local/tomcat# cd webapps
root@05b7fe07b68d:/usr/local/tomcat/webapps# ls
root@05b7fe07b68d:/usr/local/tomcat/webapps# cd ..
root@05b7fe07b68d:/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@05b7fe07b68d:/usr/local/tomcat# ll
bash: ll: command not found

# 问题:1、命令太少;2、webapps是空的;
# 原因:阿里云镜像的原因,默认是最小的镜像,把不必要的都剔除了
# 保证最小可运行环境!

# 操作一波

root@05b7fe07b68d:/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@05b7fe07b68d:/usr/local/tomcat# cd webapps.dist/
root@05b7fe07b68d:/usr/local/tomcat/webapps.dist# ls # webapps.dist下面是有东西的
ROOT docs examples host-manager manager
root@05b7fe07b68d:/usr/local/tomcat/webapps.dist# cd ..
root@05b7fe07b68d:/usr/local/tomcat# cp -r webapps.dist/* webapps # 将webapps.dist下面的文件拷贝到webapps
root@05b7fe07b68d:/usr/local/tomcat# cd webapps
root@05b7fe07b68d:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager

# 浏览器再次访问

浏览器访问:

【Docker】004.2-Docker部署Tomcat_linux

浏览器再次访问:

【Docker】004.2-Docker部署Tomcat_tomcat_02

Docker部署Tomcat并挂载目录

# 拉取镜像
docker pull tomcat

# 启动一个demo容器
docker run --name tomcat -p 8002:8080 -d tomcat

# 复制里面的内容
docker cp tomcat:/usr/local/tomcat/webapps.dist /home/docker/tomcat
# 改名
mv webapps.dist webapps
docker cp tomcat:/usr/local/tomcat/conf /home/docker/tomcat
docker cp tomcat:/usr/local/tomcat/logs /home/docker/tomcat

# 直接删除demo容器
docker rm -f tomcat

# 启动并挂载正式的tomcat
docker run -d \
--name zb-tomcat \
--restart always \
-v /home/docker/tomcat/webapps:/usr/local/tomcat/webapps \
-v /home/docker/tomcat/conf:/usr/local/tomcat/conf \
-v /home/docker/tomcat/logs:/usr/local/tomcat/logs \
-p 8002:8080 \
tomcat