1, an overview of

There are two main ways to deploy a JavaWeb project to a Linux server with Docker installed:

  1. Package the project into a War package and deploy it into the Tomcat container of Docker
  2. Package the project into a Jar package and make it into a Docker image using Docker’s JDK image and Dockerfile

2. Use the Tomcat container for deployment

Advantages: Easy to operate

Disadvantages: The project port must correspond to the Tomcat port, that is, one Tomcat container must be opened for one project

Here we go:

  1. Pull the Tomcat image

  1. Prepare the JavaWeb project

  1. Package the JavaWeb project as a War package (note that web.xml is ready)

  1. Rename the file for convenience

  1. Upload the War to the server

  1. Start the Tomcat container (note port mapping)
docker run -d -p 8080:8080 --name tomcat tomcat
Copy the code

  1. Hit the pit! The Tomcat scan directory is located in Webapps, but there are webapps and webapps.dist in the container. We need to delete webapps. Then rename webapps.dist to Webapps (because there is data in webapps.dist)

  1. Copy demo.War to the Webapps directory within the Tomcat container and restart the container

  1. test

3, use Dockerfile packaging image

Advantages: It uses the class command line startup and does not need to be deployed to Tomcat

Disadvantages: Complex operation, need to understand the use of Dockerfile

Here we go:

  1. Pull Java:8-alpine image (to generate project image)

  1. Package projects as Jar packages

  1. Upload the Jar package to the server and create a Dockerfile file in its directory (the Dockerfile file has no suffix and the name must be D uppercase and F lowercase)

  1. Run the docker command to generate the image (. Means to find the Dockerfile file in the current directory)
docker build -t springdemo:1.0 .
Copy the code

  1. View the generated image and run it

  1. test