Docker 安装 Tomcat

跑官方 Tomcat 镜像、部署 war、改端口与 JVM 参数。

Beginner
Key Pointswar 放进 webapps 目录即自动部署;端口和 JVM 参数用 CATALINA_OPTS 调。

启动

Bash
docker run -d --name tomcat -p 8080:8080 tomcat:10.1-jdk21-temurin
curl -I http://127.0.0.1:8080

部署 war

Bash
docker run -d --name tomcat -p 8080:8080 \
  -v "$PWD/app.war:/usr/local/tomcat/webapps/app.war:ro" \
  tomcat:10.1-jdk21-temurin
# 访问 http://127.0.0.1:8080/app/

Tomcat unpacks wars dropped in webapps. The official image ships an empty webapps and keeps the samples in webapps.dist, so a 404 at / is expected.

JVM 与端口

Bash
docker run -d --name tomcat -p 8080:8080 \
  -e CATALINA_OPTS="-Xms256m -Xmx512m -Dfile.encoding=UTF-8" \
  -e JAVA_OPTS="-XX:+UseG1GC" \
  tomcat:10.1-jdk21-temurin
VariablePurpose
CATALINA_OPTSTomcat process only (not the stop script)
JAVA_OPTSAlso used by other JVM invocations
JPDA_ADDRESSRemote debug port, needs catalina.sh jpda

看日志

Bash
docker logs -f tomcat
docker exec tomcat tail -f /usr/local/tomcat/logs/catalina.out
Logs go to logs/catalina.out by default, so docker logs looks empty. Enable the ConsoleHandler in logging.properties to pipe them to stdout.

Friends & Partners