使用公共tomcat镜像构建, 1、确保已经启动了dockers服务

[root@localhost ~]# systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since Sun 2019-08-11 11:23:20 CST; 21h ago Docs: http://docs.docker.com Main PID: 26466 (dockerd-current) ...

2、从公共仓库下载tomcat镜像

[root@localhost ~]# docker search tomcat -->查看公共仓库内镜像 INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/tomcat Apache Tomcat is an open source implementa... 2491 [OK]
docker.io docker.io/tomee Apache TomEE is an all-Apache Java EE cert... 66 [OK]
docker.io docker.io/dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba... 53 [OK] docker.io docker.io/bitnami/tomcat Bitnami Tomcat Docker Image 29 [OK]

[root@localhost ~]# docker pull tomcat -->我之前已经下载好了 Using default tag: latest Trying to pull repository docker.io/library/tomcat ... latest: Pulling from docker.io/library/tomcat Digest: sha256:2785fac92d1bcd69d98f2461c6799390555a41fd50d3f847b544368d594c637b Status: Image is up to date for docker.io/tomcat:latest

[root@localhost ~]# docker images -->查看本地镜像 REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/tomcat latest 238e6d7313e3 3 weeks ago 506 MB

3、运行容器

[root@localhost test]# pwd /data/test [root@localhost test]# docker run --name mytomcat -p 8080:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d tomcat
注: --name 容器的名称 -p 端口映射,将容器的8080端口映射到当前主机的8080端口上 -v 挂载目录 $PWD当前目录 将容器的test目录挂载到本地的$PWD/test上 -d 指定镜像 35468d20e2b0f16ed05153eb0ecbcb26615446e04c6cae76dd68e3c669454249 [root@localhost test]# docker ps -->名为mytomcat的容器已经启动 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 35468d20e2b0 tomcat "catalina.sh run" 6 seconds ago Up 6 seconds 0.0.0.0:8080->8080/tcp mytomcat 请求浏览器访问

4、维护

[root@localhost ~]# docker cp test/adveranaly.war d4b3b0382a72 :/usr/local/tomcat/webapps --> 上传war包 [root@localhost ~]# docker exec -it mytomcat /bin/bash -->查看容器内的war包 root@d4b3b0382a72:/usr/local/tomcat# root@d4b3b0382a72:/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 root@d4b3b0382a72:/usr/local/tomcat# cd webapps/ root@d4b3b0382a72:/usr/local/tomcat/webapps# ls adveranaly.war [root@localhost ~]# docker restart mytomcat -->重启容器 [root@localhost ~]# docker exec -it mytomcat /bin/bash
root@d4b3b0382a72:/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 root@d4b3b0382a72:/usr/local/tomcat# cd webapps/ root@d4b3b0382a72:/usr/local/tomcat/webapps# ls -->确认已经执行 ROOT adveranaly adveranaly.war