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!