How to debug an application running in Docker with IntelliJ?
In Java 8 the JDK supports a JAVA_TOOL_OPTIONS environment variable so to enable the debugger for any Java application you can add the following parameters to your docker run
command:
-p 8000:8000 -e "JAVA_TOOL_OPTIONS=\"-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n\""
Then start a remote debug session connecting to localhost:8000.
Run the docker image like below:
docker run -d -p 8080:8080 -p 5005:5005 \
-e JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n" \
imagename:tagname
For java 11 onwards replace JAVA_TOOL_OPTIONS to "-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=n"
Intellij configuration Steps:
From the menu bar click on
run
→Edit Configurations
→ in the left panel click onRemote
→ click on+
symbol to add the debug configAfter adding a new config, debug
mode=Attach
to remote JVM. Fill the host and port numberSelect the module classpath to debug then apply the settings
To connect run the above remote config from the
run
menu.
Sheesh Never mind. I didnt really need the Docker Integration plugin. Seems like that is more for deployment and management of Docker directly through Intellij than for debugging.
To debug my jetty app running inside my docker container, I simply remote debugged:
Run | Edit configurations | + | Remote
The command line args were already OK since I used the default remote debugging options. I only needed to change the Host settings. Here I used the hostname I had set within the docker container