Question
How can I automatically attach a remote debugger to a running Java application in a Docker instance?
Answer
To automate the process of attaching your debugger to the JVM running within a Dockerized environment, you can configure IntelliJ IDEA with specific settings:
-
Check the remote debugging documentation provided by IntelliJ IDEA at Debugging in Docker containers to set up an efficient environment for debugging Dockerized Java applications.
-
Adjust your Docker configuration to expose the debug port (for example,
5005) and start the JVM with the JDWP agent options (see Remote debug tutorial):-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005Use
suspend=nso the JVM starts immediately without waiting for the debugger; setsuspend=yif you need to debug application startup. -
Pass those options automatically using one of the following approaches:
Dockerfile — add the agent to your
ENTRYPOINTorCMD:ENTRYPOINT ["java", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005", "-jar", "app.jar"] EXPOSE 5005Or use the
JAVA_TOOL_OPTIONSenvironment variable, which is picked up by any JVM process regardless of how it is launched:ENV JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005" EXPOSE 5005Docker Compose — set
JAVA_TOOL_OPTIONSin the service definition:services: app: environment: - JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 ports: - "5005:5005"Once the container is running, create a Remote JVM Debug run configuration in IntelliJ IDEA (host:
localhost, port:5005) and start it — the IDE will attach to the JVM automatically.
You may also choose Docker Compose, check this link: https://www.jetbrains.com/help/idea/run-and-debug-a-spring-boot-application-using-docker-compose.html#debug_app
Comments
0 comments
Please sign in to leave a comment.