DEV Community

Cover image for Java remote debugging (IntelliJ + Docker)
Arturo Batistute
Arturo Batistute

Posted on • Updated on

Java remote debugging (IntelliJ + Docker)

If you ever need to debug a running application inside a docker container, I'm here to help you!


Youtube video

If you prefer a practical example, I create this video to help:

In this article we're going to use IntelliJ IDE as the debugger connector.


JDWP

To enable a debug port in the application, we're going to use the Java Debug Wire Protocol (JDWP) that is the communication between our IntelliJ and the JVM running inside the docker container.


Solution

The JDWP command below will work only for Java version 9+. For other versions, check the links in the end of the article.

In the command we need to set the port that will enable the debug. In this example we're using the 8000.

Using the docker scenario, lets put the JDWP command inside a .sh file:

In the JDWP command we use the the app .jar file, so don't forget to add the right .jar name here!
You can generate the .jar file using the mvn package command for example. See the link for examples: https://www.baeldung.com/java-create-jar

Call the .sh file from the docker file:

Create a docker image from the app and run a container exposing the desired ports for your application and don't forget to expose the debugger port 8000.

docker build -t java-docker:1.0 .
docker run -d -p 8080:8080 -p 8000:8000 java-docker:1.0


IntelliJ

In the IDE, add a new debugger configuration. In the Run/Debug configurations add a Remote JVM Debug:

IntelliJ IDE image of the run/debug configurations with the Remote JVM debug option

Set host and the debugger port:

IntelliJ IDE image showing where to put the host and the port

All done, now you can click in the debug button and see the magic happen.

Any problems or suggestions, feel free to get in touch.

All the best!

Links:

https://docs.oracle.com/en/java/javase/17/docs/specs/jdwp/jdwp-spec.html

https://www.baeldung.com/java-application-remote-debugging

Top comments (0)