Skip to content

Instantly share code, notes, and snippets.

@telekosmos
Created April 14, 2017 08:37
Show Gist options
  • Save telekosmos/603543c8b9262bf6668ae72a82bf1109 to your computer and use it in GitHub Desktop.
Save telekosmos/603543c8b9262bf6668ae72a82bf1109 to your computer and use it in GitHub Desktop.
Remote Debugging a Java Application Inside a Docker Container
One problem when trying to debug a Java application running inside a Docker container is:
You can't expose an additional port when re(starting) a Docker container.
Here are the steps I used to create a remote debugging session for Planets:
In our scenario we can't simply override the Dockerfile CMD or pass an environment variable JAVA_OPTS to enable remote debugging.
So we have to "enter" the container with exec and add the JAVA_OPTS to the start script there:
$ docker exec -it planets-staging /bin/bash
# vi eclipse.ini
<append JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n">
Next we create a snapshot of the container and stop it afterwards.
$ docker commit planets-staging debug/staging-snapshot
$ docker stop planets-staging
Now we can expose an additional port for remote debugging when we run the snapshot:
$ docker run -i -p 80:80 -p 8000:8000 debug/staging-snapshot
Just for completeness in case you have a scenario where you can pass JAVA_OPTS as environment to your start script.
$ docker run -i -p 80:80 \
-e "JAVA_OPTS=\"-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n\"" \
-p 8000:8000 debug/staging-snapshot
Happy remote debugging!
Pd: not tried :-S
Credits to: https://devops.datenkollektiv.de/remote-debugging-a-java-application-inside-a-docker-container.html
@pangyuteng
Copy link

@telekosmos and @pfrandsen thank you! this gist and *:8000 save my day! "big 'ol +1" indeed!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment