The main goal of this practice is to develop a simple Web application that can be packaged and deployed into the Tomcat container of Docker. Docker daoCloud = docker Hub = docker Hub = Docker Hub = Docker Hub = Docker Hub = Docker Hub = Docker daoCloud = Docker Hub = Docker daoCloud = Docker daoCloud = Docker daoCloud = Docker daoCloud = Docker daoCloud = Docker daoCloud Hub.docker.com, suggest you to register an account above, so you have your own warehouse can save the image.
Search for Tomcat on hub.docker.com and the first result is the official image, as shown below:
Click on the Detail button to go to the details page, and you can see that there are several tags, such as 7.0.75, which is the version of tomcat7.0.75:
So many versions, which one to choose? Let’s first take a look at a few specific version of the difference, open this link under tomcat’s official website: tomcat.apache.org/whichversio…
Specific differences can be seen:
As you can see, tomcat7 supports servlet3.0, which can meet our requirements, so to use it, execute the following command to download the image:
Docker pull tomcat: 7.0.75Copy the code
Command execution may fail, retry several times, after successful pull docker images command can see the image:
To quickly experience the effect of mirroring, run the following command:
Docker run it --rm -p 8888:8080 tomcat:7.0.75Copy the code
– Rm indicates that when the Container ends,Docker automatically clears the data generated by the container.
You can see that the tomcat startup logs are all printed on the terminal.
Since we used -p 8888:8080 to map port 8080 of the container to port 8888 of the current computer, open the browser of the current computer, type :localhost:8888, and you can see the familiar big cat:
Next, we will develop a simple Spring MVC application and deploy it to the Tomcat container of Docker. We will use IntelliJ IDEA CE to create maven project:
GAV information is as follows:
Add an MVN command here, as shown below:
Add MVN command:
The configuration command is as follows:
Next we will add Spring MVC support to the Web project, starting with the web. XML file, replaced with the following:
<? The XML version = "1.0" encoding = "utf-8"? > <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version = "3.1" metadata-complete="true"> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>Copy the code
The complete POM file reads as follows:
The < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Bolingcavalry < / groupId > < artifactId > helloworldwebapp < / artifactId > <packaging> War </packaging> <version> 1.0-snapshot </version> <name> HelloWorldWebApp Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId> servlet-API </artifactId> <version>2.5</version> <scope> Provided </scope> </dependency> <dependency> < the groupId > org. Springframework < / groupId > < artifactId > spring - webmvc < / artifactId > < version > 4.3.4. RELEASE < / version > </dependency> </dependencies> <build> <finalName>helloworldwebapp</finalName> </build> </project>Copy the code
When compiling, I encountered a small problem that needs to be mentioned here. Right click on the project to view module properties, as shown below:
The information you see is as follows:
Note the green box in the image above. If you don’t see anything in the green box in your project, right click on the red box and click “Sources” from the pop-up menu to add the Java directory to your project’s compile directory.
In this case, the Java file still cannot be compiled by executing the MVN command. Right-click the project and execute the MVN reimport command as shown in the following figure. After executing the command, the Java file can be compiled by using the MVN command:
Now add the test code, first add a view directory, which put a JSP file, file structure and JSP file content as follows:
Add a Java file with the following path:
The source of this file:
package com.bolingcavalry.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class FirstController { @RequestMapping(value = "firstview", method = RequestMethod.GET) public String index() { return "firstview"; }}Copy the code
Add the spring-servlet. XML file to the webapp/WEB-INF directory as follows:
<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <! --> <context:component-scan base-package=" com.bolingCavalry.*" /> <! < MVC :annotation-driven /> <! MVC :default-servlet-handler /> <! - view the parser - > < bean class = "org. Springframework. Web. Servlet. The InternalResourceViewResolver" > < property name = "prefix" value="/view/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>Copy the code
Now that the code is written and ready to be packaged, execute the command we just configured, as shown below:
After executing the command, you can see the war package in the target directory:
Now to deploy the files to Tomcat, create a directory. For example, I created this directory: / Users/bolingcavalry/temp / 201703/10 / share, and then put the helloworldwebapp. The war file to the directory, and then in the console executing the following command:
docker run --name helloworldwebapp -p 8888:8080 -d -v /Users/bolingcavalry/temp/201703/10/share:/usr/Downloads Tomcat: 7.0.75Copy the code
To start a container, execute the following command to enter the container
docker exec -it helloworldwebapp /bin/bashCopy the code
After entering the container, run the following command to copy the WAR package to the Tomcat container directory:
cp /usr/Downloads/helloworldwebapp.war /usr/local/tomcat/webapps/Copy the code
Try this time to open a browser, type http://localhost:8888/helloworldwebapp/firstview, as the chart, in line with expectations:
Enter exit to exit the container, execute “Docker stop HelloWorldWebApp” to stop the container, and then run the following command to save the container as an image:
Docker commit -A "BolingCavalry" -M "from Tomcat 7.0.75,with a demo webApp "HelloWorldWebApp Bolingcavalry/helloworldwebapp: 0.0.1Copy the code
-a: author -m: description at the time of submission 0.0.1: tag
After executing, type docker images and you can see the new image:
Next we will try to submit the local image to hub.docker.com (if you have already registered with the site), enter the command docker login, enter the username and password as prompted, and execute the following command to submit the image:
Docker push bolingcavalry/helloworldwebapp: 0.0.1Copy the code
A little time consuming and need to wait:
After successfully uploading, go to Hub.docker.com and you can see the image in your own repository: