If you ever need to debug a running application inside a docker container, I'm here to help you!
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.
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.
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
In the IDE, add a new debugger configuration. In the Run/Debug configurations add a Remote JVM Debug:
Set host and the debugger 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!