Remote debugging a Java Application running in Docker container with Intellij Idea

Sushil Kumar
The Startup
Published in
4 min readJul 21, 2019

I’ve learnt it the hard way that not every bug in your code can be traced by adding println statements. Attaching a debugger to your running code can sometimes magically help you to see what’s wrong with your code.

Debugging meme

Remote debugging was baked into Java since earlier versions, but some of the major improvements around “Full Speed Debugging” were introduced in Java 1.4.0 and hasn’t changed much since. You can take a look the docs for Java 8 here.

I recently stumbled upon the remote debugging capabilities of Intellij Idea and wanted to test it. In this post I’m going to share my setup of how I deployed a long running minimalist java application on a Cloud VM in a docker container and debugged it using my laptop.

Lets get started.

First lets create a Java application which keeps on printing increasing number indefinitely with a delay of 5 seconds between each number.

Package the application in a jar. I’m using maven, but you can use your preferred way to package the jar.

Now lets create a docker file for this application.

The thing to note is the 3rd line where I’m setting JAVA_TOOL_OPTIONS environment variable. This variable tells the JRE that it has to enable JPDA session so that the application can be debugged remotely using Java Debug Wire Protocol (JWDP).

Lets create the docker file. Make sure your jar and Dockerfile are in the same directory.

docker build -t remote-debugger:0.1 .

Run the application.

docker run -d -p 8000:8000 remote-debugger:0.1

This will start your application in daemon mode and also will expose 8000 port on host machine.

You’ll see a log line like below.

If you don’t see this please make sure you have your environment variable correctly set. For Java versions prior to 8 the variable was called JAVA_OPTS.

I’m running this on a cloud VM but it could be any server as long as it allows inbound connection on whatever port you have set in the environment variable. Also your application does not necessarily have to run inside a container. All the steps in this post applies to normally deployed java applications as well.

Now lets configure remote debugging in Intellij.

Goto Run > Debug > Edit Configurations

Debug Configuration

Click + and add a remote configuration.

Configuring Remote Debug session
  • Select Attach to remote JVM for Debugger Mode.
  • Enter IP address of remote host and Port that you configured.
  • Hit Apply.

For more configuration options read here.

Add a break-point to the print statement.

Goto Run > Debug and select the configuration that we just created. It should start the debugger.

You’ll see following logs.

Remote session connected

Intellij will load all the variables and call stack in the window.

Debug window

You can see the value of all the variables in the scope. We only have i having value 134. To double check that you are actually debugging the remote version of the app. Goto the logs of your application, you’ll see they have stopped progressing and are waiting for debugger to move to next statement.

Application Logs

And there you have it. We have successfully setup a remote debugging session for our Java application.

In case you have any doubt or you find any error in the code, please feel free to drop a comment below.

Till then happy coding :)

--

--

Sushil Kumar
The Startup

A polyglot developer with a knack for Distributed systems, Cloud and automation.