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.

在这里插入图片描述

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐