Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll see how to debug a Docker container in IntelliJ. We assume that we have a Docker image ready for testing. There are various ways to build Docker images.

IntelliJ can be downloaded from its official website.

For this article, we’ll refer to this single class-based Java application. It can be easily dockerized, built, and tested.

Before starting the testing, we need to make sure that the Docker engine is started and running on our computer.

2. Using the Dockerfile Configuration

When using the Docker file configuration, we need to simply select our Dockerfile and provide appropriate names for image name, image tag, container name, and configuration name. We may also add port mappings, if any:

configuration using docker file

Once this configuration is saved, we can select this configuration from the debug option and hit debug. It first builds the image, registers the image in the Docker engine, and then runs the dockerized application.

3. Using the Docker Image Configuration

When using the Docker image configuration, we need to provide the image name, image tag, and container name of our application that we’ve pre-built. We can use standard Docker commands to build the image and register the container in the Docker engine. We may also add port mappings, if any:

configuration using docker image

Once this configuration is saved, we can select this configuration from the debug option and hit debug. It simply selects the pre-built Docker image and container and runs it.

4. Using the Remote JVM Debug Configuration

A remote JVM configuration attaches itself to any pre-running Java process. So we need to run the Docker container individually first.

Here’s the command to run the Docker image for Java 8:

docker run -d -p 8080:8080  -p 5005:5005 -e JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n" docker-java-jar:latest

If we were using Java 11, we’d use this command instead:

docker run -d -p 8080:8080  -p 5005:5005 -e JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=n" docker-java-jar:latest

Here docker-java-jar is our image name, and latest is its tag. Apart from the normal HTTP port, which is 8080, we are also mapping an additional port, 5005, for remote debugging using the -p extension. We are using the -d extension for running docker in detached mode and -e for passing JAVA_TOOL_OPTIONS as an environment variable to the Java process.

In the JAVA_TOOL_OPTIONS we pass the value -agentlib:jdwp=transport=dt_shmem,address=,server=y,suspend=n to allow the Java process to start a JDB debug session and pass the value address=*:5005 to specify that 5005 will be our remote debugging port.

So the above command starts our Docker container, and we can now configure remote debugging configuration to connect to it:

configuration using remote jvm debug

We can see that in the configuration we’ve specified it to connect to the remote JVM using the 5005 port.

Now, if we select this configuration from the debug options and click debug, it will start a debug session by attaching to the already running Docker container.

5. Conclusion

In this article, we learned different configuration options we can use to debug a dockerized application in IntelliJ.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.