How to Run sample.war in a Tomcat Docker Container

Running a sample.war file in a Tomcat Docker container is straightforward with the right configuration. Here's a step-by-step guide:

1. Prepare Your Project

Ensure you have the following ready:

  • A sample.war file (your Java web application archive - for example Tomcat doc sample).
  • A Dockerfile to configure the Docker container.

2. Dockerfile Configuration

Create a Dockerfile in the same directory as your sample.war file with the following content:

复制代码
FROM tomcat:11.0.2

ADD sample.war /usr/local/tomcat/webapps/

EXPOSE 8080
CMD ["catalina.sh", "run"]

Explanation:

  • FROM tomcat:11.0.2 Uses the official Tomcat 11.0.2 image as the base.
  • ADD sample.war /usr/local/tomcat/webapps/ Adds the sample.war file to the Tomcat webapps directory for deployment.
  • EXPOSE 8080 Exposes port 8080 for accessing the application.
  • CMD "catalina.sh", "run" Starts Tomcat in the foreground.

3. Build the Docker Image

Run the following command to build the Docker image:

复制代码
docker build -t tomcat-sample .

Here:

  • -t tomcat-sample specifies the name of the image.

4. Run the Docker Container

Start the container using:

复制代码
docker run -d -p 8080:8080 --name tomcat-sample-container tomcat-sample

Explanation:

  • -d Runs the container in detached mode.
  • -p 8080:8080 Maps port 8080 of the container to port 8080 on the host.
  • --name tomcat-sample-container Assigns a name to the container.
  • tomcat-sample Specifies the image to use.

5. Access the Application

Once the container is running, access your application in a web browser at: http://localhost:8080/sample

Replace localhost with the appropriate IP address if running on a remote server.

6. Verify the Deployment

You should see your application's homepage or a Tomcat message confirming successful deployment.

相关推荐
杨运交4 小时前
[069][公共模块]Spring Boot 全局异常处理与参数校验实战(下):校验异常精细化处理与 WebFlux 适配
java·spring boot·后端
广州灵眸科技有限公司5 小时前
瑞芯微(EASY EAI)RV1126B 星闪使用
运维·人工智能·科技·docker·容器
努力努力再努力wz5 小时前
【Docker入门系列】:从 Namespace 到 CGroup:一文理解容器资源控制、cgroup v2 与 Memory Controller 实战
运维·docker·容器
Raas1005 小时前
MAI Gateway(魔芋企业级AI网关)对比分析:AI网关和OpenRouter区别?企业级能力差距一览
java·服务器·网络·人工智能·gateway·ai网关·mai gateway
APItesterCris5 小时前
告别人工盯品!借助 Open‑Claw 快速搭建电商商品全自动监控与数据分析系统(完整实操代码)
java·大数据·前端·数据库
青山木5 小时前
Hot 100 --- 跳跃游戏 II
java·数据结构·算法·leetcode·贪心算法
ly76895 小时前
Spring 中的 @Configuration 与 @Component 差异:为何代理时机决定 Bean 生命周期行为
java·后端·spring·注解·代理·bean生命周期
qq_452396235 小时前
第十二篇:《数据采集:Grafana Alloy、Fluent Bit、Vector 的选型与配置》
java·贪心算法·grafana
11路没有终点5 小时前
Docker 化测试环境:一致性交付
运维·docker·容器
珍珠先生5 小时前
P16 · IDEA 启动报错:找不到或无法加载主类
java