-
-
Save telekosmos/603543c8b9262bf6668ae72a82bf1109 to your computer and use it in GitHub Desktop.
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 |
That only works if your container is running Tomcat because JAVA_OPTS is a Tomcat specific environment variable that it's launcher scripts pass on to the JVM.
In Java 8 the JDK supports a JAVA_TOOL_OPTIONS environment variable so to enable the debugger for any Java application you need to use:
-e "JAVA_TOOL_OPTIONS=\"-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n\""
In Java 10, the JDK supports a JDK_JAVA_OPTIONS (JDK-8170832) environment variable for memory settings and such.
I have found that I explicitly need to bind to all interfaces to make it work, either address=0.0.0.0:8000 or address=*:8000
I have found that I explicitly need to bind to all interfaces to make it work, either address=0.0.0.0:8000 or address=*:8000
This works!!!
I have found that I explicitly need to bind to all interfaces to make it work, either address=0.0.0.0:8000 or address=*:8000
This works!!!
I'll give a big 'ol +1 on this one: added *:8000 was the only thing that worked for me
@telekosmos and @pfrandsen thank you! this gist and *:8000 save my day! "big 'ol +1" indeed!!
didn't work