Deploy and Debug Tomcat with Docker in IDEA

Build Docker Image

Create docker-compose.yml and edit like following codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
tomcat:
image: "tomcat:latest"
ports:
- "127.0.0.1:8000:8000"
- "127.0.0.1:8888:8080"
environment:
- JPDA_ADDRESS=8000
- JPDA_TRANSPORT=dt_socket
volumes:
- /path/to/war/file/directory:/usr/local/tomcat/webapps
container_name: tomcat-literature
command: "/usr/local/tomcat/bin/catalina.sh jpda run"
restart: always

The key point in the config file is the two environment variables:

1
2
JPDA_ADDRESS=8000
JPDA_TRANSPORT=dt_socket

Caveat

You may set the JPDA_ADDRESS value *:8000, which makes Tomcat listen on 0.0.0.0 other than 127.0.0.1.

Run Docker Container

In the directory of docker-compose.yml, execute command:

1
docker-compose up -d

Debug in IDEA

  1. add a Remote Run Configuration

  1. set a breakpoint in your program anywhere you like

  2. swith to Run Configuration which setup in first step, and click debug