In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to write a custom Dockerfile tomcat image and realize the online deployment of web applications. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
At first, the way to deploy web applications on tomcat is through the maven-compiler-plugin plug-in of maven into a war package, and then copy the war package to the webapps directory of tomcat, and then use the tomcat7-maven-plugin plug-in, which can be compiled, packaged and deployed directly on maven. The key to this method is to create a user account on tomcat, and then the maven plug-in uses this account and password to perform online deployment. In this practice, we are going to create an image. The image run is a tomcat server, and this server supports the maven plug-in to deploy war applications online. The first is to create the maven project, which is a spring mvc project, and the tomcat7-maven-plugin plug-in is used in the pom file. The code in the project is very simple. There is only one controller, which returns the hello page. The specific code can be downloaded from my git, the address is: https://github.com/zq2599/blog_demos, you can also refer to the command git clone git@github.com:zq2599/blog_demos.git to get. After downloading, there are multiple projects in the whole folder. Loadbalancedemo is used in this actual operation, as shown in the red box below. 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 bottom plugin node:
Org.apache.tomcat.maventomcat7-maven-plugin2.2 http://localhost:8080/manager/texttomcat7/${project.artifactId}true
That's all for the code, and then we're going to make the docker image file. Let's do the preparatory work first:
Create an empty folder, such as image_tomcat
Download the tomcat installation package from the tomcat official website. After decompression, 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, and add the attribute URIEncoding= "UTF-8" to the Connector node where port equals 8080, as shown below:
The purpose of this change is to encode the parameters in the get request with UTF-8, so that we can enter Chinese parameters directly in the browser's address bar. After the change, the node becomes:
Modify the tomcat-users.xml file to add content to the tomcat-users node:
The modified effect is shown in the following figure, with the new content in the red box:
With these configurations, the account001 account can be used for online deployment on the corresponding tomcat.
All right, when the preparation work is over, we can start to make docker image files. In fact, it is very simple to make a Dockerfile file, and then execute this file through the docker command to generate an image. We add a file named "Dockerfile" in the image_tomcat directory using the editor, and the contents are as follows:
# First docker file from bolingcavalry# VERSION 0.0.roomAuthor: bolingcavalry# basic image FROM tomcat:7.0.77-jre8# author MAINTAINER BolingCavalry # definition working directory ENV WORK_PATH/ usr/local/tomcat/conf# definition filename to replace ENV USER_CONF_FILE_NAME tomcat-users.xml# definition server.xml filename to replace ENV SERVER_CONF_FILE_NAME server.xml# delete the original file tomcat-users.xmlRUN rm $WORK_PATH/$USER _ CONF_FILE_NAME# copy file tomcat-users.xmlCOPY. / $USER_CONF_FILE_NAME $WORK_PATH/# delete the original file server.xmlRUN rm $WORK_PATH/$SERVER_CONF_FILE_NAME# copy file server.xmlCOPY. / $SERVER_CONF_FILE_NAME $WORK_PATH/
These are all the contents of Dockerfile. The key points are as follows:
FROM tomcat:7.0.77-jre8 means to use tomcat:7.0.77-jre8 as the base image, and 7.0.77-jre8 is tag
After using tomcat:7.0.77-jre8 as the basic image, delete the original server.xml and tomcat-users.xml files through the RUN rm command
Copy the server.xml and tomcat-users.xml files we prepared in the preparation work to the original conf directory, thus replacing the original files with
Open the computer's command line and go to the image_tomcat directory, where there are only three files:
Execute the command:
Docker build-t bolingcavalrytomcat:0.0.1.
There will be similar output during execution:
After the execution is completed, enter docker images to view the current local image, as shown in the following figure. You can see the new image: with the image, execute the following command to start tomcat:
Docker run-- name=tomcat001-p 8080 TOMCAT_SERVER_ID=tomcat_server_001-idt bolingcavalrytomcat:0.0.1
The parameter-e TOMCAT_SERVER_ID=tomcat_server_001 means that the environment variable TOMCAT_SERVER_ID is set in the container, and the value is tomcat_server_001. When you execute the docker ps command, you can see the launched container:
Visit localhost:8080 with a browser and you can see a familiar page:
Now that tomcat is started and the code is written, can you try to deploy online? Don't worry, the last step is to open the maven installation directory, find the settings.xml file in the conf directory, and add the following to the servers node after opening it:
Tomcat7 account001 password001
This is the user and password configured in the tomcat-users.xml file of tomcat, so that when you execute the maven plug-in, you can get the corresponding user name and password from here to tomcat to do the operation.
Go back to the previous web project, use the command line to enter the directory where the pom.xml file is located, and execute the command
Mvn clean package-U-Dmaven.test.skip=true tomcat7:redeploy
The implementation results are as follows:
The ok,tomcat7-maven-plugin plug-in has helped us deploy war to the docker container, enter it in the browser
Http://localhost:8080/loadbalancedemo/hello?name= Zhang San
You can see the following effect:
The blue font tomcat_server_001 is the environment variable set by the-e parameter when we start the tomcat container, which is successfully extracted in HelloController and displayed on the jsp page, as shown in the following code:
Above, we have obtained our own image file through some file operations on the official tomcat image, and combined with the mvn plug-in to achieve the effect of online deployment of web applications on the tomcat container.
The above content is how to write Dockerfile custom tomcat image and realize the online deployment of web applications. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.