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.

相关推荐
splage2 小时前
Spring Framework 中文官方文档
java·后端·spring
洋不写bug2 小时前
Java线程(二):线程特点、状态、终止开始控制(
java·开发语言
sonnet-10292 小时前
拓扑排序的实现
java·c语言·开发语言·笔记·算法
不想看见4042 小时前
Min Stack栈和队列--力扣101算法题解笔记
java·笔记·leetcode
马猴烧酒.2 小时前
【面试八股|计算机网络】计算机网络常见面试题详解笔记
java·开发语言·网络·笔记·计算机网络·算法·面试
千码君20162 小时前
kotlin:函数式参数
java·算法·kotlin
西门吹雪分身2 小时前
JMM java内存模型分析
java·开发语言
回到原点的码农2 小时前
Spring Boot 热部署
java·spring boot·后端