Live Docker, write Dockerfile custom tomcat8 image for Web application online deployment

Source: Internet
Author: User
Tags docker ps docker run

Tag: color img command to execute UIL span user Images address installation package

The first way to deploy a web app on Tomcat was to make a war package with the Maven Maven-compiler-plugin plugin and then copy the war package into the Tomcat WebApps directory. Later with the Tomcat7-maven-plugin plug-in, can be directly on Maven compiled, packaged, deployment one-time completion, the key is to create a user account on Tomcat, and then the MAVEN plugin with this account and password to perform online deployment.

In this practice, we're going to create an image that, after run, is a Tomcat server that supports the MAVEN plugin to deploy the war application online.

The first is to create a MAVEN project, which is a spring MVC project, and the Pom file uses the Tomcat7-maven-plugin plug-in, the project code is very simple, only a controller, return to the Hello page, The specific code can be downloaded in my git, the address is: Https://github.com/zq2599/blog_demos, you can also refer to the execution of the command git clone [email protected]:zq2599/blog_ Demos.git to obtain, after downloading the whole folder under a number of projects, the actual combat use is Loadbalancedemo, proposed with INTELLJ idea to the form of MAVEN project to import.

Open the Pom.xml file in your project and you can see the value of the bottom-most plugin node:

           <plugin>                <groupId>org.apache.tomcat.maven</groupId>                <artifactId> tomcat7-maven-plugin</artifactid>                <version>2.2</version>                <configuration>                    <url>http://192.168.1.129:8080/manager/text</url>                    <server>tomcat8</server>                    <path>/${project.artifactId}</path>                    <update>true</update>                </configuration >            </plugin>

To start making a Docker image file, let's get the job done first:
1. Create an empty folder, such as Image_tomcat;
2. Go to the Tomcat website to download the Tomcat installation package, unzip, the Conf folder under the Server.xml and tomcat-users.xml the two files copied to the front of the new Image_tomcat directory;
3. Modify the Server.xml file, as below, at port equal to 8080 connector node to increase the property uriencoding= "UTF-8":

<connector port= "8080" protocol= "http/1.1"               connectiontimeout= "20000"               redirectport= "               8443" uriencoding= "UTF-8"/>

The purpose of this change is to UTF-8 the parameters in the GET request so that we can enter Chinese parameters directly in the browser's address bar.

4. Modify the Tomcat-users.xml file to add content to the Tomcat-users node:

<role rolename= "manager"/>    <role rolename= "Manager-gui"/> <role rolename=    "admin"/>    <role rolename= "Admin-gui"/>    <user username= "Tomcat" password= "Tomcat" roles= "Admin-gui,admin, Manager-gui,manager-script,manager "/>   

With these configurations, the Tomcat account can be used for online deployment on the corresponding Tomcat;

OK, the preparation is finished, we can start to do the Docker image file, in fact, do the image is very simple, is to do the Dockerfile file, and then the Docker command to execute the file, you can generate the image, we in the Image_tomcat directory with the editor to add a file, File name "Dockerfile" with the following contents:

# first Docker file from lz# VERSION 0.0.1# author:lz# base image from tomcat# author maintainer lizhong <[email protected]> #定义工作 Directory env work_path/usr/local/tomcat/conf# definition manager.xml working directory env manager_path/usr/local/tomcat/conf/catalina/localhost #定义要替换的文件名ENV user_conf_file_name tomcat-users.xml# Define the Server.xml file name to replace Env Server_conf_file_name server.xml# Define the Manager.xml file name to be added env manager_conf_file_name manager.xml# Delete original file Tomcat-users.xmlrun rm $WORK _path/$USER _conf_file_ name# copy Files Tomcat-users.xmlcopy  ./$USER _conf_file_name $WORK _path/#删除原文件server. Xmlrun RM $WORK _path/$SERVER _ conf_file_name# copy Files Server.xmlcopy  ./$SERVER _conf_file_name $WORK _path/#复制文件manager. Xmlcopy  ./$MANAGER _ Conf_file_name $MANAGER _path/

The above is the whole content of Dockerfile, several key points are as follows:
1. From Tomcat means using Tomcat as the base image;
2. After using Tomcat as the base image, the original server.xml and Tomcat-users.xml files are deleted by the run RM command;
3. Copy the Server.xml and Tomcat-users.xml files prepared in our preparation to the original Conf directory, thus replacing the original documents;

Open the computer's command line, enter the Image_tomcat directory, this directory only these four files:

Execute command:

Docker build-t tomcat:0.0.1.

There will be similar output in execution:

After execution, enter Docker images to view the current native image, as you can see the new image:

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

Docker run--name=tomcat001-p 8080:8080-e Tomcat_server_id=tomcat_server_001-idt tomcat: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 at this point, you can see the boot container:

To access 192.168.1.129:8080 in a browser, you can see the familiar pages:

Now that Tomcat is up and the code is written, can you try deploying it online? Don't worry, just take the last step, open the MAVEN installation directory, locate the settings.xml file in the Conf directory, and then add the following on the Servers node when you open it:

<server>    <id>tomcat8</id>    <username>tomcat</username>    <password >tomcat</password></server>

This is the user and password configured in Tomcat's Tomcat-users.xml file, so that when the Maven plugin is executed, it can get the corresponding username and password from here to Tomcat.

Back to the previous Web project, use the command line to enter the directory where the Pom.xml file is located, execute the command:

MVN Clean package-u-dmaven.test.skip=true Tomcat7:redeploy

The results of the implementation are as follows:

The Ok,tomcat7-maven-plugin plugin has helped us deploy the war to the Docker container, and in the browser input:

Http://192.168.1.129:8080/loadbalancedemo/hello?name= Zhang Sanfeng

You can see the following effects:

The tomcat_server_001 of the blue font is the environment variable set by the-e parameter when we launch the Tomcat container, which is successfully removed from the Hellocontroller and displayed on the JSP page, as shown in the code:

The above through some of the official Tomcat image of the file operation, we have made the image of our own files, and combined with the MVN plug-in implementation of the Web application online deployment on the Tomcat container effect.

Live Docker, write Dockerfile custom tomcat8 image for Web application online deployment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.