To deploy web applications on Tomcat, maven’s Maven-Compiler-plugin first created a WAR package, and then copied the WAR package to tomcat’s webapps directory. Later, tomcat7-Maven-plugin was used. You can compile, package, and deploy directly on Maven. The key is to create a user account on Tomcat, and the Maven plugin will use this account and password to perform online deployment.

In this practice, we will create an image, which when run is a Tomcat server, this server supports maven plug-in to deploy war application online.

This project is a spring MVC project, and poM file using tomcat7-Maven-plugin, project code is very simple, only a controller, return hello page, specific code can be downloaded in git. The address is:Github.com/zq2599/blog…Git clone [email protected]:zq2599/blog_demos. Git can also be obtained by running git clone [email protected]:zq2599/blog_demos. It is recommended to import intellJ Idea in the form of Maven project:

Open the pom.xml file in the project and you can see the value of the plugin node at the bottom:

< plugin > < groupId > org, apache tomcat, maven < / groupId > < artifactId > tomcat7 maven - plugin < / artifactId > < version > 2.2 < / version >  <configuration> <url>http://localhost:8080/manager/text</url> <server>tomcat7</server> <path>/${project.artifactId}</path> <update>true</update> </configuration> </plugin>Copy the code

The code side is here first, next we start to make docker image file, let’s do the preparatory work:

1. Create an empty folder, such as image_tomcat.

2. ToTomcat’s official websiteDownload the Tomcat installation package, decompress it, and copy the server. XML and tomcat-users. XML files in the conf folder to the newly created image_tomcat directory.

Modify the server. XML file as shown in the following figure. Add URIEncoding= “UTF-8” to the Connector with port equal to 8080, as shown in the following figure:

This change is used to encode the parameters in the GET request with UTF-8 encoding, so that we can enter the Chinese parameters directly in the browser address bar.

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               URIEncoding="UTF-8" />Copy the code
  1. Modify the tomcat-users. XML file and add the following contents to the tomcat-Users node:
<role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <user username="account001" password="password001" roles="manager-gui,manager-script"/>Copy the code

The modified effect is shown below, with the new content in the red box:

With these configurations, you can deploy the Account001 account online on the corresponding Tomcat.

In fact, it is very simple to make a good Dockerfile, and then execute the file through the docker command, can generate an image, we use the image_tomcat directory under the editor to add a file, The file name is Dockerfile and the content is as follows:

# First Docker file from BolingCavalry # VERSION 0.0.1 # Author: Bolingcavalry # Base Mirroring FROM tomcat:7.0.77-jre8 # author MAINTAINER BolingCavalry <[email protected]> # Define the working directory ENV WORK_PATH /usr/local/tomcat/conf # Define the file name to be replaced ENV USER_CONF_FILE_NAME tomcat-users. XML # Define the file name to be replaced server. XML ENV XML # Delete the original file tomcat-users. XML RUN rm $WORK_PATH/$USER_CONF_FILE_NAME # Copy the file tomcat-users.xml COPY./$USER_CONF_FILE_NAME $WORK_PATH/ # Delete the original file server. XML RUN rm $WORK_PATH/$SERVER_CONF_FILE_NAME # COPY the file server. XML ./$SERVER_CONF_FILE_NAME $WORK_PATH/Copy the code

Use tomcat:7.0.77-jre8 as the base image, 7.0.77-jre8 as the tag, 2. After tomcat:7.0.77-jre8 is used as the basic image, RUN the RUN rm command to delete the original server. XML and tomcat-users. XML files. 3. Copy the server. XML and tomcat-users. XML files prepared in the preparation to the original conf directory, thus replacing the original files.

Open the command line on your computer and go to the image_tomcat directory, which contains only these three files:

Execute command:

Docker build -t bolingcavalrytomcat: 0.0.1.Copy the code

There is output like this in execution:

After the execution, enter Docker images to view the image of the current machine, as shown below, you can see the new image:



With an image, you can start Tomcat by executing the following command:

Docker run - name = tomcat001 -p 8080:8080 - e TOMCAT_SERVER_ID = tomcat_server_001 - idt bolingcavalrytomcat: 0.0.1Copy the code

The -e TOMCAT_SERVER_ID=tomcat_server_001 parameter means that the environment variable TOMCAT_SERVER_ID is set in the container and the value is tomcat_server_001

Docker ps: docker ps:

Use a browser to access localhost:8080 and you’ll see the familiar page:

Now that Tomcat is up and the code is written, can you try online deployment? Go to maven’s installation directory, find settings. XML in the conf directory, and add the following to the Servers node:

<server>
       <id>tomcat7</id>
       <username>account001</username>
       <password>password001</password>
     </server>Copy the code

This is the user and password configured in the tomcat-users. XML file of Tomcat, so that when implementing the Maven plug-in, you can obtain the corresponding user name and password to operate on Tomcat.

Go back to the previous Web project, use the command line to go to the directory where the POM.xml file is located, and execute the command

mvn clean package -U -Dmaven.test.skip=true tomcat7:redeployCopy the code

The result is as follows:

The Tomcat7-Maven-plugin has already deployed war to the Docker container. Type it in the browser

http://localhost:8080/loadbalancedemo/hello?name= zhang SANCopy the code

You can see the following effect:

Tomcat_server_001 in blue is the environment variable set by -e parameter when we start the Tomcat container. It is successfully removed from HelloController and displayed on the JSP page. The code is shown as follows:

We can use the MVN plugin to realize the effect of online deployment of web applications on the Tomcat container. Next time, we will follow this example to start two Tomcat containers, and then put a nginx in front of it. Docker compose, docker compose, Docker Compose, Docker Compose, Docker Compose, Docker Compose, Docker Compose, Docker Compose