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.

相关推荐
麻瓜生活睁不开眼3 小时前
Android16修改全局桌面视图边框四直角显示为弧边圆角
android·java·深度学习
dear_bi_MyOnly3 小时前
【MyBatis 操作数据库】
java·数据库·学习·mybatis·学习方法
2601_953720823 小时前
【计算机毕业设计】基于Spring Boot的心理健康咨询与评测系统的设计与实现
java·spring boot·后端
丈剑走天涯4 小时前
JDK 17 正式特性
java·开发语言
圣光SG5 小时前
Java操作题练习(七)
java·开发语言·算法
晚风吹长发6 小时前
Docker基础——Docker Network(网络详解)
linux·运维·网络·ubuntu·docker·容器
@航空母舰6 小时前
SpringBoot通过Map实现天然的策略模式
java·spring boot·后端
Co_Hui7 小时前
Java 并发编程
java
long3168 小时前
Java 新手入门与实战开发指南
java·开发语言
执笔画流年呀8 小时前
Linux搭建Java项目部署环境
java·linux·运维