Skip to content

Debugging apps running in Docker with JetBrains and VSCode: Java, C, and Python

If you have a setup to deploy your application using containers, debugging your application by connecting to debugging server that is running in your development docker container has the following advantages:

  • Environment consistency: minimize environment-related bugs.
  • Isolation of dependencies: less clutter in your local machine, avoid version conflicts.
  • Easy cleanup and reset: restart, delete or stop your container as necessary.

The goal of this guide is to display how to remotely debug an application written in Java, Python, or C running inside a docker container. We will use CLion, IntelliJ IDEA, and VSCode to demonstrate this. We do not cover PyCharm in the guide, because at the time of writing, the remote debugging feature is limited to the paid version of their software.

Each section includes a sample application for those without an existing project who still want to follow along.

Although this document covers remote debugging with three popular languages using three IDEs, the key patterns in remote debugging are the same for any environment:

  1. Find out whether your IDE can connect to a debugging server.

  2. Learn which debugging tool your IDE uses. For example, PyCharm uses pydevd-pycharm and VSCode uses debugpy for Python by default.

  3. Adjust or create a Docker image that exposes the debugging server over a network.

  4. Configure your IDE to use the debugging server exposed by a Docker container for debugging.

After studying the examples in this guide, you should be able to adapt them to your own needs. For example, you can use delve debugger to debug Go applications using GoLand. And as an alternative to IntelliJ IDEA, use Eclipse for Java debugging and so forth.

The next parts demonstrate how to use popular IDEs to debug applications written in Java, Python, or C.

We will use a sample Spring application, Spring PetClinic, to explain how to remotely debug Java docker applications. Spring PetClinic was dockerized using Liberica as a base image.

We will also use IntelliJ IDEA, as it is popular among Java developers.

  • Docker installed and running
  • Installed IntelliJ IDEA

Spring PetClinic is a CRUD application that uses technologies such as Spring Boot, Thymeleaf, Bootstrap, in-memory database H2.

Overall, it is a well-known and suitable application for use as an example.

Clone the PetClinic application from GitHub

Section titled “Clone the PetClinic application from GitHub”

Use the following command to clone the sample application.

Terminal window
git clone https://github.com/spring-projects/spring-petclinic.git

Place the contents of the Dockerfile below into a file Dockerfile inside the spring-petclinic directory we have just cloned.

# Create a stage for resolving and downloading dependencies.
FROM bellsoft/liberica-openjdk-alpine:21 AS deps
# Download dependencies as a separate step to take advantage of Docker's
# caching.
WORKDIR /build
COPY --chmod=0755 mvnw mvnw
COPY .mvn/ .mvn/
RUN --mount=type=bind,source=pom.xml,target=pom.xml \
--mount=type=cache,target=/root/.m2 \
./mvnw dependency:go-offline -DskipTests
# Create a stage for building the application based on the stage with
# downloaded dependencies.
FROM deps AS package
WORKDIR /build
COPY ./src src/
RUN --mount=type=bind,source=pom.xml,target=pom.xml \
--mount=type=cache,target=/root/.m2 \
./mvnw package -DskipTests && \
mv target/$(./mvnw help:evaluate -Dexpression=project.artifactId \
-q -DforceStdout)-$(./mvnw help:evaluate -Dexpression=project.version \
-q -DforceStdout).jar \
target/app.jar
# Create a stage for extracting the application into separate layers.
FROM package AS extract
WORKDIR /build
RUN java -Djarmode=layertools -jar target/app.jar \
extract --destination target/extracted
# Create a new stage for running the application that contains the minimal
# runtime dependencies. We use liberica-openjre-alpine:21
# because there is no need to use a full blown JDK just to run the app.
FROM bellsoft/liberica-openjre-alpine:21 AS final
# Create a non-privileged user that the app will run under.
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
USER appuser
# Copy the executable from the "package" stage.
COPY --from=extract build/target/extracted/dependencies/ ./
COPY --from=extract build/target/extracted/spring-boot-loader/ ./
COPY --from=extract build/target/extracted/snapshot-dependencies/ ./
COPY --from=extract build/target/extracted/application/ ./
# Expose 8080, as we will use this as the application port.
EXPOSE 8080
# Specifying the command that will executed when the container starts.
ENTRYPOINT [ "java", "org.springframework.boot.loader.launch.JarLauncher" ]

Use the following commands to build the petclinic-liberica image.

Terminal window
cd spring-petclinic
docker build --tag petclinic-liberica .

In this section we have created the example production image that we want to debug.

To debug the PetClinic application, we use Java Debug Wire Protocol (JDWP), which is the protocol used for communication between a debugger (the IDE) and the Java virtual machine (the PetClinic app). It helps to perform debugging tasks such as setting breakpoints, stepping through code, and inspecting variables in IntelliJ IDEA.

Liberica and many other docker java images come with JDWP; therefore, you do not need to install other tools. We will instruct JVM to use JDWP using the command line arguments.

JAVA_TOOL_OPTIONS environment variable can be used to specify command line options for java launcher. The content of the JAVA_TOOL_OPTIONS environment variable is a list of arguments separated by space.

We can append a command line option required to start java debugging server to JAVA_TOOL_OPTIONS.

The option needed by java launcher is the following:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005

Copy the contents of the Dockerfile above into a file Dockerfile.debug in the spring-petclinic directory.

# Change this to your production image
FROM petclinic-liberica
# Add command line option for enabling JDWP debugging server
ENV JAVA_TOOL_OPTIONS="${JAVA_TOOL_OPTIONS} \
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"

Use the following commands to build the debugging image. Remember to expose the port that was specified, in this case 5005, defined in the JAVA_TOOL_OPTIONS environment variable.

Terminal window
docker build --tag petclinic-liberica-debug -f Dockerfile.debug .
docker run -p8080:8080 -p5005:5005 petclinic-liberica-debug
Picked up JAVA_TOOL_OPTIONS: -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005
Listening for transport dt_socket at address: 5005
...

As stated in the output, the application is listening for a debugging session on port 5005.

Before proceeding, ensure the debugging container is running and the Java debugging server is set up, waiting at port 5005.

  1. Open the previously cloned spring-petclinic directory using IntelliJ IDEA by clicking File > Open and then selecting the path to spring-petclinic directory. Click OK.

    intellij open project

  2. Click Open as Maven project and click Trust Project. You should be now in the spring-petclinic project.

  3. From the main menu, select Run > Edit Configurations.

    intellij edit config

  4. Click ”+” (plus) on the top left to add a new configuration and select Remote JVM Debug.

    intellij add config remote jvm

    You can give this configuration a meaningful name, such as “Docker Pet Clinic Debugging”.

    intellij remote jvm debug settings

    Note that the default values for Host and Port are localhost and 5005 respectively. Update these to match the host address and port configured in the previous steps if you changed the values.

  5. Click OK to save the configuration.

To ensure debugging is working as expected, let’s insert a breakpoint at the processCreationForm method of the OwnerController class.

intellij add breakpoint to processCreationForm

Let’s try to debug the application using the configuration we have created.

From the main menu, click Run > Debug ‘Docker Pet Clinic Debugging’ (or select the name assigned to the debug configuration earlier).

intellij run debugging

You should see a message in the debugging console that you are connected to localhost:5005 or your specified port over the network.

intellij debugging connected

processCreationForm method is responsible for handling POST requests to the endpoint /owners/new. It manages the creation of a new Owner entity, validates the form input, and redirects users based on the result of the validation.

Let’s try to debug that method.

  1. In your browser, go to http://localhost:8080 and click Add Owner on the Find Owners section.

    petclinic add owner

  2. Fill the form and click Add Owner.

    petclinic add owner form

    Now the web page waits, because we have instructed the debugger to stop at the processCreationForm method in the OwnerController class.

    We can see that the method first instantiates an owner object with the data we provided and then saves it to the owners which is a Spring Repository.

    We can step through the code, set other breakpoints, evaluate expressions, and more.

  3. Click Continue to let the application continue normally.

    intellij debugging continue

    Now we see that the application continues and finishes the POST request normally. Every time the application runs the code we marked with a breakpoint, it stops and waits for an input in IntelliJ IDEA, so we can debug it further.

    petclinic owner created