DEV Community

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

Posted on • Edited on

9 1

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:

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000 -jar remote-debugging.jar
view raw entrypoint.sh hosted with ❤ by GitHub

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:

# syntax=docker/dockerfile:1
FROM eclipse-temurin:17-jdk-jammy
EXPOSE 8080 8000
ADD target/remote-debugging.jar remote-debugging.jar
ADD entrypoint.sh entrypoint.sh
ENTRYPOINT ["sh","/entrypoint.sh"]
view raw Dockerfile hosted with ❤ by GitHub

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

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more