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:

  1. From the menu bar click on runEdit Configurations → in the left panel click on Remote → click on + symbol to add the debug config

  2. After adding a new config, debug mode=Attach to remote JVM. Fill the host and port number

  3. Select the module classpath to debug then apply the settings

  4. To connect run the above remote config from the run menu.

example config


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