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.

相关推荐
谢亮_vipxieliang20 小时前
容器日志收集与管理:从 stdout 规范到 ELK/Loki 落地
运维·网络·人工智能·elk·docker·容器
边境悍匪20 小时前
蜗牛学苑 Java 智能体学习 Day49|贯穿项目 5 订单下单业务 思维导图复盘
java·开发语言·spring boot·学习·阿里云
鬼手点金20 小时前
Claude Code示范案例-修复 Bug 工作流
java·服务器·前端·javascript·bug·openclaw
一条破秋裤20 小时前
Linux 进程通信介绍:内存隔离与 IPC 分类
java·linux·服务器
羑悻的小杀马特20 小时前
Docker高阶实战:从Redis集群到C++微服务,全面解析镜像优化与生产环境部署+镜像制作常见问题详解
c++·redis·docker·镜像制作·dockefile
机核研创社20 小时前
polo 长袖短裤套装自动化产线:八台机器排队接力
android·java·自动化
你不是我我20 小时前
【AI 测评】群晖NAS部署CloudSaver:Docker安装、多源搜索、网盘转存与cpolar远程访问
运维·docker·容器
Roadinforest20 小时前
Spring Boot 4 六步入门实录:从 Hello World 到一个能交付的 Todo API
java·开发语言·spring boot·spring
半仙白桑20 小时前
内核篇第十三讲:system-V消息队列
java·linux·网络
今天的砖头有点烫手啊20 小时前
GC 算法与垃圾收集器演进:从 Serial 到 ZGC
java·jvm