Create a java web application development environment with docker for mac part2

Introduction

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.

  1. [Enable tomcat's web application manager for deployment. ](# Enable tomcat's web application manager for deployment)
  2. [Create a sample war for deployment. ](#Create a sample war for deployment)
  3. Deploy war. ](Deploy #war)
  4. [Connect the debugger to the deployed web application and stop the operation. ](# Connect the debugger to the deployed web application and stop the operation)

Enable tomcat web application manager for deployment

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.

Extract tomcat config file

Find out the container name of tomcat running on docker

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.

Go into the container and find out the path to the tomcat config file

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 tomcat config file from docker 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 and add an account for deployment

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 and mount the extracted conf directory

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.

Confirm the user of the created tomca

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. スクリーンショット 2017-08-06 15.33.35.png

Now you can deploy war using tomcat's web application manager.

Create a sample war for deployment

This time I will use the spring-boot sample. Anything that can create a war will do.

https://spring.io/guides/gs/spring-boot/

Project setup

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.

Changed spring-boot to create war

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 class

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);
    }

}

Modify pom.xml

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

create a war

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] ------------------------------------------------------------------------

deploy war

Add maven plugin for deploying to tomcat to pom.xml

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.

Set the credentials that maven's tomcat plugin uses for deployment

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>

Deploy

console


$ mvn tomcat7:deploy -Dmaven.test.skip=true

Check the deployed war with the web application manager

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.

スクリーンショット_2017-08-06_17_18_03.png

Try to access the deployed war

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.

スクリーンショット 2017-08-06 17.21.49.png

Connect the debugger to the deployed web application and stop the operation

Set the debugger port and connection address in the docker-compose tomcat startup settings

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.

In the IDE, create a remote connection configuration for the debugger.

  1. Select Run> Edit Configurations .. from the IntelliJ menu
  2. The Run / Debug Configurations panel opens, press the + button.
  3. Select "Remote" from Add New Configuration
  4. Set "tomcat.docker" to Host in Settings
  5. Set Port in Settings to "8000"

With the above settings, it will look like this.

スクリーンショット 2017-08-06 18.41.35.png

Make a remote connection for the debugger

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'

Set a break pointer and check

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

Create a java web application development environment with docker for mac part2
Creating a java web application development environment with docker for mac part1
Build a web application development environment that uses Java, MySQL, and Redis with Docker CE for Windows
[For beginners] Until building a Web application development environment using Java on Mac OS
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Build a development environment for Docker, java, vscode
Create a Spring Boot development environment with docker
[Note] Create a java environment from scratch with docker
Create a Vue3 environment with Docker!
Build Java development environment (for Mac)
I tried to create a java8 development environment with Chocolatey
Web application development environment construction in Java (for inexperienced people)
Java web application development environment construction with VS Code (struts2)
Until you create a Web application with Servlet / JSP (Part 1)
I tried to create a padrino development environment with Docker
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
Create a web environment quickly using Docker
Create a simple web application with Dropwizard
Create a MySQL environment with Docker from 0-> 1
Create Spring Boot-gradle-mysql development environment with Docker
Let's create a Java development environment (updating)
Build a Java development environment on Mac
Build a Wordpress development environment with Docker
Build a development environment for Django + MySQL + nginx with Docker Compose
Build a development environment for Docker + Rails6 + Postgresql
[Memo] Create a CentOS 8 environment easily with Docker
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Build a WordPress development environment quickly with Docker
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
Prepare a scraping environment with Docker and Java
Create a docker environment for Oracle 11g XE
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Build a Java development environment with VS Code
# 1 [Beginner] Create a web application (website) with Eclipse from knowledge 0. "Let's build an environment for creating web applications"
Build a development environment to create Ruby on Jets + React apps with Docker
Build Java development environment with VS Code on Mac
Create a Java, JavaScript team development environment (problem raising)
Create a JAVA WEB application and try OMC APM
[Environment construction] Build a Java development environment with VS Code!
Try to build a Java development environment using Docker
Create Chisel development environment with Windows10 + WSL2 + VScode + Docker
Java development environment (Mac, Eclipse)
Web application built with docker (1)
Build a local development environment for Open Distro for Elasticsearch with multiple nodes using Docker
Docker × Java Building a development environment that is too simple
Deploying a Java environment with Windows Subsystem for Linux (WSL)
Tutorial to create a blog with Rails for beginners Part 1
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Try developing a containerized Java web application with Eclipse + Codewind
Tutorial to create a blog with Rails for beginners Part 2
Introducing Spring Boot2, a Java framework for web development (for beginners)
Create a Java and JavaScript team development environment (gradle environment construction)
Tutorial to create a blog with Rails for beginners Part 0
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Deploy a Docker application with Greengrass
Build a Node.js environment with Docker
Environment construction for Servlet application development
Environment construction with Docker for beginners
Prepare Java development environment with Atom
Build a web application with Javalin