Creating a java web application development environment with docker for mac part1

Introduction

For the time being, the goal is to be able to create a development environment, and it is not intended for production operation using docker.

Specifically, run tomcat8 on docker in the local development environment, The goal is to deploy war for webapp from IDE etc. and check the operation.

In addition, the explanation of the installation procedure such as docker for mac, docker command, docker-compose command required for these operations is omitted.

This time, we will do the following two things.

  1. Create a dinghy-http-proxy environment
  2. Run tomcat in docker environment

I will explain about deploying web applications next time.

Required packages and commands

--docker for mac (application) --docker-compose (command) --Use it because it is convenient to pass various parameters to the docker container. --Install with brew

Confirmed version information

docker version
Client:
 Version:      17.03.1-ce
 API version:  1.27
 Go version:   go1.7.5
 Git commit:   c6d412e
 Built:        Tue Mar 28 00:40:02 2017
 OS/Arch:      darwin/amd64

Server:
 Version:      17.03.1-ce
 API version:  1.27 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   c6d412e
 Built:        Fri Mar 24 00:00:50 2017
 OS/Arch:      linux/amd64
 Experimental: true
docker-compose --version
docker-compose version 1.11.2, build dfed245

Create a dinghy-http-proxy environment

https://github.com/codekitchen/dinghy-http-proxy

This is a reverse proxy and DNS server for docker environment. If you put this in and set it, you will be able to access the tomcat container in the docker environment with the URL http://tomcat.docker/. It's easy to understand.

Start dinghy-http-proxy

docker run -d --restart=always \
  -v /var/run/docker.sock:/tmp/docker.sock:ro \
  -v ~/.dinghy/certs:/etc/nginx/certs \
  -p 80:80 -p 443:443 -p 19322:19322/udp \
  -e DNS_IP=127.0.0.1 -e CONTAINER_NAME=http-proxy \
  --name http-proxy \
  codekitchen/dinghy-http-proxy

Check that the dinghy-http-proxy process is running with docker ps

docker ps
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                                                                           NAMES
96067c3ea144        codekitchen/dinghy-http-proxy   "/app/docker-entry..."   47 seconds ago      Up 45 seconds       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

Add settings to the DNS resolver for mac.

Create a file named "docker" in / etc / resolver / and Please describe the following contents. Now when there is a docker domain query, the query will go to the dinghy-http-proxy DNS server.

nameserver 127.0.0.1
port 19322

Run php with docker and check the operation of dinghy-http-proxy.

docker run -it --name testphp --expose 80 --env VIRTUAL_HOST=testphp.docker php php -S 0.0.0.0:80

Try accessing testphp.docker in your browser and It's OK if the log appears on the console.

PHP 7.1.3 Development Server started at Sat Apr 15 02:53:30 2017
Listening on http://0.0.0.0:80
Document root is /
Press Ctrl-C to quit.
[Sat Apr 15 02:53:44 2017] 172.17.0.2:40528 [404]: / - No such file or directory
[Sat Apr 15 02:53:45 2017] 172.17.0.2:40530 [404]: /favicon.ico - No such file or directory

After checking, send CTRL-C to the console that started php to exit.

The container used for confirmation is also unnecessary, so delete it.

docker rm testphp 

Run tomcat in docker environment

Create docker-compose file

Create an empty directory and create the following docker-compose.yml file in it.

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"
  command: catalina.sh jpda run

9090 is a JMX connection port, 8000 is the port for connecting to the debugger (JPDA). Since the 8080 port connects via dinghy-http-proxy, it is not necessary to specify publicity in ports.

The environment variables VIRTUAL_HOST and VIRTUAL_PORT are settings for dinghy-http-proxy. Now when you open http://tomcat.docker/ in your browser from mac It connects to port 8080 of the docker container.

Start tomcat with docker-compose

Move to the same directory as docker-compose.yml and execute the following command to start the tomcat8 container.

docker-compose up
Starting tomcat8docker_tomcat_1
Attaching to tomcat8docker_tomcat_1
tomcat_1  | Listening for transport dt_socket at address: 8000
tomcat_1  | 15-Apr-2017 03:39:25.168 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.0.43
tomcat_1  | 15-Apr-2017 03:39:25.173 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Mar 28 2017 14:42:59 UTC
tomcat_1  | 15-Apr-2017 03:39:25.174 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.0.43.0
tomcat_1  | 15-Apr-2017 03:39:25.175 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
tomcat_1  | 15-Apr-2017 03:39:25.175 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            4.9.13-moby
tomcat_1  | 15-Apr-2017 03:39:25.177 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
tomcat_1  | 15-Apr-2017 03:39:25.177 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/lib/jvm/java-8-openjdk-amd64/jre
tomcat_1  | 15-Apr-2017 03:39:25.178 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_121-8u121-b13-1~bpo8+1-b13
tomcat_1  | 15-Apr-2017 03:39:25.182 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
tomcat_1  | 15-Apr-2017 03:39:25.182 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
tomcat_1  | 15-Apr-2017 03:39:25.183 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
tomcat_1  | 15-Apr-2017 03:39:25.190 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
tomcat_1  | 15-Apr-2017 03:39:25.192 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
tomcat_1  | 15-Apr-2017 03:39:25.193 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
tomcat_1  | 15-Apr-2017 03:39:25.194 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
tomcat_1  | 15-Apr-2017 03:39:25.196 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -agentlib:jdwp=transport=dt_socket,address=localhost:8000,server=y,suspend=n
tomcat_1  | 15-Apr-2017 03:39:25.197 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote
tomcat_1  | 15-Apr-2017 03:39:25.198 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=9090
tomcat_1  | 15-Apr-2017 03:39:25.200 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.rmi.port=9090
tomcat_1  | 15-Apr-2017 03:39:25.201 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
tomcat_1  | 15-Apr-2017 03:39:25.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.local.only=false
tomcat_1  | 15-Apr-2017 03:39:25.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.authenticate=false
tomcat_1  | 15-Apr-2017 03:39:25.204 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=tomcat.docker
tomcat_1  | 15-Apr-2017 03:39:25.205 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.endorsed.dirs=/usr/local/tomcat/endorsed
tomcat_1  | 15-Apr-2017 03:39:25.206 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
tomcat_1  | 15-Apr-2017 03:39:25.206 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
tomcat_1  | 15-Apr-2017 03:39:25.208 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
tomcat_1  | 15-Apr-2017 03:39:25.211 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.12 using APR version 1.5.1.
tomcat_1  | 15-Apr-2017 03:39:25.212 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
tomcat_1  | 15-Apr-2017 03:39:25.224 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.1.0e  16 Feb 2017)
tomcat_1  | 15-Apr-2017 03:39:25.402 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"]
tomcat_1  | 15-Apr-2017 03:39:25.428 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"]
tomcat_1  | 15-Apr-2017 03:39:25.434 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 785 ms
tomcat_1  | 15-Apr-2017 03:39:25.494 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
tomcat_1  | 15-Apr-2017 03:39:25.504 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.43
tomcat_1  | 15-Apr-2017 03:39:25.527 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/host-manager
tomcat_1  | 15-Apr-2017 03:39:26.172 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 645 ms
tomcat_1  | 15-Apr-2017 03:39:26.175 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/ROOT
tomcat_1  | 15-Apr-2017 03:39:26.227 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/ROOT has finished in 52 ms
tomcat_1  | 15-Apr-2017 03:39:26.230 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/manager
tomcat_1  | 15-Apr-2017 03:39:26.293 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 63 ms
tomcat_1  | 15-Apr-2017 03:39:26.294 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/docs
tomcat_1  | 15-Apr-2017 03:39:26.353 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 60 ms
tomcat_1  | 15-Apr-2017 03:39:26.355 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/examples
tomcat_1  | 15-Apr-2017 03:39:26.700 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/examples has finished in 345 ms
tomcat_1  | 15-Apr-2017 03:39:26.707 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"]
tomcat_1  | 15-Apr-2017 03:39:26.728 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"]
tomcat_1  | 15-Apr-2017 03:39:26.752 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1317 ms

Try to display the tomcat page

Try to display the following URL in your browser.

http://tomcat.docker/

Did you see the tomcat welcome page?

スクリーンショット 2017-04-15 12.42.11.png

Try connecting to JMX

jconsole tomcat.docker:9090

Did you check the memory usage status with jconsole?

jconsole2.png

jconsole1.png

Regarding JMX connection to tomcat running in docker container

The following two system properties are important:

I referred to the following article. http://d.hatena.ne.jp/Kazuhira/20141112/1415782056

com.sun.management.jmxremote.rmi.port

JMX remote connection uses two port numbers, and one of them has an undefined port number, so if there is a firewall etc., it was not possible to connect before java7update4.

Basically, docker can not connect unless you explicitly specify the port and publish it, so Specify this property to fix the port used for JMX connection, and then You need to map that port and expose it to the mac side.

I referred to the following article. http://tech.furyu.jp/blog/?p=3467

java.rmi.server.hostname

https://ptmccarthy.github.io/2014/07/24/remote-jmx-with-docker/

And

https://forums.docker.com/t/enable-jmx-rmi-access-to-a-docker-container/625

According to the JavaVM startup system property java.rmi.server.hostname It seems that you should set the IP address of docker in.

In this case, since docker is running on the local machine, it is OK if you specify 127.0.0.1, Even if you specify tomcat.docker, it will be converted to 127.0.0.1 by DNS query, so it seems that either one is fine.

Recommended Posts

Creating a java web application development environment with docker for mac part1
Create a java web application development environment with docker for mac part2
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
Build Java development environment (for Mac)
Web application development environment construction in Java (for inexperienced people)
Java web application development environment construction with VS Code (struts2)
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
Laravel development environment construction with Docker (Mac)
Build a PureScript development environment with Docker
Build a Java development environment on Mac
Build a Wordpress development environment with Docker
(For myself) Try creating a C # environment with docker + code-server, cloud9
Build a development environment for Django + MySQL + nginx with Docker Compose
Build a development environment for Docker + Rails6 + Postgresql
I tried to build a Firebase application development environment with Docker in 2020
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
Prepare a scraping environment with Docker and Java
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Create a Spring Boot development environment with docker
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"
Create a Java development environment using jenv on Mac
Build Java development environment with VS Code on Mac
[Note] Create a java environment from scratch with docker
Build Java development environment with WSL2 Docker VS Code
[Environment construction] Build a Java development environment with VS Code!
Try to build a Java development environment using Docker
Creating a lightweight Java environment that runs on 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)
I tried to create a java8 development environment with Chocolatey
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Until you create a Web application with Servlet / JSP (Part 1)
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Try developing a containerized Java web application with Eclipse + Codewind
Introducing Spring Boot2, a Java framework for web development (for beginners)
I tried to create a padrino development environment with Docker
Create a Vue3 environment with Docker!
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
Web application creation with Nodejs with Docker
[Java & SpringBoot] Environment Construction for Mac
Java development environment (Mac, VS Code)
Install Java development environment on Mac
Comparison of WEB application development with Rails and Java Servlet + JSP
Let's make a book management web application with Spring Boot part3
Let's make a book management web application with Spring Boot part2
Prepare a transcendentally simple PHP & Apache environment on Mac with Docker
[Probably the easiest] WEB application development with Apache Tomcat + Java Servlet
AWS Elastic Beanstalk # 1 with Java starting from scratch-Building a Java web application environment using the EB CLI-