Implementation of deploying war package project using Docker

Implementation of deploying war package project using Docker

To deploy war with Docker, you must use a container. We use the tomcact container. In fact, we just drop the war package into the webapps directory of tomcat. When tomcat is started, it will automatically decompress the war package.

One is to install the tomcat container image in Docker, and then drop the war package into webapps under the tomcat image. However, if tomcat is closed, the package under its webapps will disappear; the second is to use mounting, still install the image of the tomcat container, but do not drop the war package into the webapps under tomcat, directly create an external folder, associate this external folder with wabapps, so that when the war is dropped into the newly created folder, tomcat's webapps can also read the war package, which is mounting.

1. Install and start the tomcat image

1. Search for the tomcat image under docker, provided that docker is in the started state. I will not explain here how to start docker and how to set it to start automatically.

docker search tomcat 

2. Download docker by name, docker pull docker.io/tomcat

3. Start tomcat

 docker run -d -p 8088:8080 docker.io/tomcat

-d means running in the background, -p means port mapping, the front 8088 is the external access port (that is, the port open to the outside world by the local IP), and the back 8080 is the port inside the docker container.

4. Browse to see if Tomcat is started

3. Transform the springboot project into a war package project

I'm used to using springboot, and it's troublesome to create a war package project. So I created a new springboot project and then changed it to a war project. If you already have a war package project, you can ignore this step.

1. Create a springboot project. I won’t talk about this. If you need it, you can read my other blog: Create and use SpringBoot simply

2. Modify the pom.xml file

2.1 Change jar to war

2.2 Remove the built-in web module's own tomcat

2.3 Add servlet dependency, otherwise packaging will report an error

<dependency>
  <groupId>javax.servlet</groupId>
	<artifactId>javax.servlet-api</artifactId>
	<version>3.1.0</version>
</dependency>

2.4 Add an alias to the project. Add finalName under the build tag, which is the project name. If you do not add it, the project name will be your artifactId-version, and the generated war package name will also be artifactId-version.war. The project name will be too long when accessed by a browser. This step depends on personal preference

3. Modify the startup class, the startup class inherits SpringBootServletInitializer

4. Put the war package under the webapps of the tomcat container and start the war package

1. Create a folder to store the uploaded war package. I put it in the root directory/

mkdir warPackage, and then transfer the file to this directory

2. Copy the war package into the webapps directory of the tomcat container

2.1. You need to know the container ID of the running tomcat container and run docker ps

2.2. Copy the war package into the webapps directory of the tomcat container. The command format is: docker cp xxx.war package path container ID:/directory path to be copied

docker cp /warPackage/dockerProject.war e591e16899c6:/usr/local/tomcat/webapps

2.3. Check whether it has been copied

Enter the tomcat container in docker: docker exec -it e591e16899c6 /bin/bash

Exit the container: exit

3. Restart the container: docker restart + container ID

4. The browser access path is: the port you just accessed: project name/interface name. My project name is dockerProject. Docker is a simple Controller I wrote.

5. Start the war package using the mount directory

Mounting is to create a separate directory on the server and then map it to the path of webapps under tomcat, so that tomcat can read files in the external folder

1. To save trouble, I will directly use the /warPackage folder created above

2. Mount the directory, run the tomcat container, and set it to restart automatically: --restart=always

docker run -d -p 8088:8080 -v /warPackage/:/usr/local/tomcat/webapps --restart=always docker.io/tomcat

Then by entering the tomcat directory, you can see that it is already under webapps

3. Browser access

This is the end of this article about using Docker to deploy war package projects. For more relevant content about Docker deployment of war packages, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to deploy stand-alone Pulsar and clustered Redis using Docker (development artifact)
  • How to connect idea to docker to achieve one-click deployment
  • Example of how to deploy MySQL 8.0 using Docker
  • Best Practices for Deploying ELK7.3.0 Log Collection Service with Docker
  • Detailed process of installing and deploying onlyoffice in docker

<<:  A more elegant error handling method in JavaScript async await

>>:  Detailed explanation of MySQL alter ignore syntax

Recommend

Specific usage of Vue's new toy VueUse

Table of contents Preface What is VueUse Easy to ...

MySQL 5.7 mysql command line client usage command details

MySQL 5.7 MySQL command line client using command...

A simple example of creating a thin line table in html

Regarding how to create this thin-line table, a s...

Web page printing thin line table + page printing ultimate strategy

When I was printing for a client recently, he aske...

MySQL 8.0.11 Community Green Edition Installation Steps Diagram for Windows

In this tutorial, we use the latest MySQL communi...

Summary of the minesweeping project implemented in JS

This article shares the summary of the JS mineswe...

Centos7 installation of FFmpeg audio/video tool simple document

ffmpeg is a very powerful audio and video process...

Play mp3 or flash player code on the web page

Copy code The code is as follows: <object id=&...

Summary of the 10 most frequently asked questions in Linux interviews

Preface If you are going to interview for a Linux...

Explanation of the concept and usage of Like in MySQL

Like means "like" in Chinese, but when ...

Solution to the problem of var in for loop

Preface var is a way to declare variables in ES5....

How to implement nginx smooth restart

1. Background During the server development proce...

Docker exec executes multiple commands

The docker exec command can execute commands in a...