In part1, I went to the point of running tomcat with docker. This time I would like to proceed to deploying and debugging the web application (war).
This time, we will do the following four things. Use maven to build and deploy the war. Use IntelliJ IDEA for debugging.
You need to enable tomcat web application manager for deploying war, To do this, you need to modify the tomcat configuration file "tomcat-users.xml" to define the user and password.
When using docker, it seems good to mount the changed configuration file on the container and read it without changing the image when modifying the configuration file etc., so I am doing so.
console
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1bf4ec739fab tomcat:8.0-jre8 "catalina.sh jpda run" 31 minutes ago Up 16 minutes 0.0.0.0:8000->8000/tcp, 0.0.0.0:9090->9090/tcp, 8080/tcp qiitadockertomcat8_tomcat_1
96067c3ea144 codekitchen/dinghy-http-proxy "/app/docker-entry..." 3 months ago Up About an hour 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 19322/tcp, 0.0.0.0:19322->19322/udp http-proxy
In this case, qiitadockertomcat8_tomcat_1
is the name of the container where tomcat is running.
Now that we know the container name, let's go inside the container and take a look at the contents to find out which path to copy from.
console
$ docker exec -it qiitadockertomcat8_tomcat_1 bash
root@1bf4ec739fab:/usr/local/tomcat# ls -la
total 128
drwxr-sr-x 1 root staff 4096 Apr 3 20:02 .
drwxrwsr-x 1 root staff 4096 Apr 3 20:02 ..
-rw-r--r-- 1 root root 57011 Mar 28 14:45 LICENSE
-rw-r--r-- 1 root root 1444 Mar 28 14:45 NOTICE
-rw-r--r-- 1 root root 6741 Mar 28 14:45 RELEASE-NOTES
-rw-r--r-- 1 root root 16195 Mar 28 14:45 RUNNING.txt
drwxr-xr-x 2 root root 4096 Apr 3 20:03 bin
drwxr-xr-x 1 root root 4096 Aug 6 05:10 conf
drwxr-sr-x 3 root staff 4096 Apr 3 20:02 include
drwxr-xr-x 2 root root 4096 Apr 3 20:02 lib
drwxr-xr-x 1 root root 4096 Aug 6 05:10 logs
drwxr-sr-x 3 root staff 4096 Apr 3 20:02 native-jni-lib
drwxr-xr-x 2 root root 4096 Apr 3 20:02 temp
drwxr-xr-x 7 root root 4096 Mar 28 14:44 webapps
drwxr-xr-x 1 root root 4096 Aug 6 05:10 work
root@1bf4ec739fab:/usr/local/tomcat# ls -la conf
total 228
drwxr-xr-x 1 root root 4096 Aug 6 05:10 .
drwxr-sr-x 1 root staff 4096 Apr 3 20:02 ..
drwxr-xr-x 3 root root 4096 Aug 6 05:10 Catalina
-rw------- 1 root root 12767 Mar 28 14:45 catalina.policy
-rw------- 1 root root 7299 Mar 28 14:45 catalina.properties
-rw------- 1 root root 1577 Mar 28 14:45 context.xml
-rw------- 1 root root 3387 Mar 28 14:45 logging.properties
-rw------- 1 root root 6458 Mar 28 14:45 server.xml
-rw------- 1 root root 2164 Mar 28 14:45 tomcat-users.xml
-rw------- 1 root root 2634 Mar 28 14:45 tomcat-users.xsd
-rw------- 1 root root 168378 Mar 28 14:45 web.xml
root@1bf4ec739fab:/usr/local/tomcat# exit
The tomcat config file folder in docker looks like / usr / local / tomcat / conf
.
Now that we know the path, exit the container.
Extract the conf folder from tomcat of docker container with the following command. The current directory when executing the command is the folder where dokcer-compose.yml is located.
console
$ docker cp qiitadockertomcat8_tomcat_1:/usr/local/tomcat/conf conf
Check the extracted conf directory.
console
$ ls
conf docker-compose.yml
$ ls -la conf
total 432
drwxr-xr-x 11 cnaos staff 374 8 6 14:10 .
drwxr-xr-x 4 cnaos staff 136 8 6 15:03 ..
drwxr-xr-x 3 cnaos staff 102 8 6 14:10 Catalina
-rw------- 1 cnaos staff 12767 3 28 23:45 catalina.policy
-rw------- 1 cnaos staff 7299 3 28 23:45 catalina.properties
-rw------- 1 cnaos staff 1577 3 28 23:45 context.xml
-rw------- 1 cnaos staff 3387 3 28 23:45 logging.properties
-rw------- 1 cnaos staff 6458 3 28 23:45 server.xml
-rw------- 1 cnaos staff 2164 3 28 23:45 tomcat-users.xml
-rw------- 1 cnaos staff 2634 3 28 23:45 tomcat-users.xsd
-rw------- 1 cnaos staff 168378 3 28 23:45 web.xml
Modify the extracted tomcat-users.xml as follows.
You need to create a user with the roles manager-gui
and manager-script
.
Please rewrite the password part by yourself and set it.
tomcat-users.xml
<!--
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
<user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
<user username="role1" password="<must-be-changed>" roles="role1"/>
-->
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="tomcat" password="[Please set an appropriate password]" roles="manager-gui,manager-script"/>
</tomcat-users>
Modify docker-compose.yml as follows. Volumes are added before command.
docker-compose.yml
tomcat:
image: tomcat:8.0-jre8
ports:
- "8000:8000"
- "9090:9090"
environment:
VIRTUAL_HOST: tomcat.docker
VIRTUAL_PORT: 8080
CATALINA_OPTS: "-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9090
-Dcom.sun.management.jmxremote.rmi.port=9090
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.authenticate=false
-Djava.rmi.server.hostname=tomcat.docker"
volumes:
- ./conf:/usr/local/tomcat/conf
command: catalina.sh jpda run
After fixing it, once you type CTRL-C on the console running docker-compose, close the container,
Start the container again with docker-compose up
.
http://tomcat.docker/manager/html/list
Go to and enter the user and password you set earlier.
It is OK when the following screen is displayed.
Now you can deploy war using tomcat's web application manager.
This time I will use the spring-boot sample. Anything that can create a war will do.
https://spring.io/guides/gs/spring-boot/
Git clone the spring-boot project to a different folder than the one containing docker-compose.yml.
console
git clone https://github.com/spring-guides/gs-spring-boot.git
Open the gs-spring-boot / complete folder as a project in the IDE.
In the default state of spring-boot, war is not created, so change it to create war.
Reference: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file
Modify Application.java as follows: Please rewrite the original Application.java entirely.
Application.java
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Add the following settings to pom.xml above the build tag
pom.xml
<packaging>war</packaging>
Add the following to the dependency of pom.xml
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
reference: http://qiita.com/ARS_2000/items/3f191aeb6f161101d5f6
In the gs-spring-boot / complete directory, enter the following command to create a war.
console
$ mvn war:war
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-spring-boot 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-war-plugin:2.6:war (default-cli) @ gs-spring-boot ---
[INFO] Packaging webapp
[INFO] Assembling webapp [gs-spring-boot] in [/Users/cnaos/workbench/gs-spring-boot/complete/target/gs-spring-boot-0.1.0]
[INFO] Processing war project
[INFO] Webapp assembled in [269 msecs]
[INFO] Building war: /Users/cnaos/workbench/gs-spring-boot/complete/target/gs-spring-boot-0.1.0.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.288 s
[INFO] Finished at: 2017-08-06T16:25:04+09:00
[INFO] Final Memory: 19M/178M
[INFO] ------------------------------------------------------------------------
pom.xml
<build>
<plugins>
...
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<server>tomcat-docker</server>
<url>http://tomcat.docker/manager/text</url>
</configuration>
</plugin>
...
</plugins>
</build>
The value inside the server tag of the configuration is used to retrieve the user ID and password for deployment described in settings.xml that will appear later.
The value inside the url tag of the configuration is the URL of tomcat's web application manager. The default value is `` `http: // localhost: 8080 / manager / text```, but change it because it will be deployed to tomcat on docker.
There is a hidden directory ".m2" for maven under the home directory. Add the following settings to settings.xml in this .m2 directory. If there is no settings.xml file, create a settings.xml file.
For the password, set the password set in tomcat-users.xml.
xml:~/.m2/settings.xml
<settings>
<servers>
<server>
<id>tomcat-docker</id>
<username>tomcat</username>
<password>【tomcat-users.Password set in xml]</password>
</server>
</servers>
</settings>
console
$ mvn tomcat7:deploy -Dmaven.test.skip=true
http://tomcat.docker/manager/html/list
When you access, you will see a list of deployed web applications, so make sure that `` `gs-spring-boot``` is deployed.
As it is from the path column on the left end of the web application manager
Now that you have access to the war, click on the / gs-spring-boot
link.
Greetings from Spring Boot!
Should be displayed.
8080
To the next line of
#### **` 8000`**
```jpda_address
To add.
#### **`docker-compose.yml`**
```xml
tomcat:
image: tomcat:8.0-jre8
ports:
- "8000:8000"
- "9090:9090"
environment:
VIRTUAL_HOST: tomcat.docker
VIRTUAL_PORT: 8080
JPDA_ADDRESS: 8000
CATALINA_OPTS: "-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9090
-Dcom.sun.management.jmxremote.rmi.port=9090
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.authenticate=false
-Djava.rmi.server.hostname=tomcat.docker"
volumes:
- ./conf:/usr/local/tomcat/conf
command: catalina.sh jpda run
Once added, restart docker-compose.
With the above settings, it will look like this.
Executes the remote connection of the created debugger.
If the following is displayed on the debugger console, the debugger connection is successful.
Connected to the target VM, address: 'tomcat.docker:8000', transport: 'socket'
Place a break pointer in the index method of HelloController.java. From the browser http://tomcat.docker/gs-spring-boot/ If you try to access and stop at the break pointer, you are successful.
Recommended Posts