Remote debug Spring Boot app in Docker using VS Code
Problem that we want to solve, we running spring boot app using a container during development, we want to find a way to setting breakpoints in a Spring Boot app running in a container (connect the IDE to the app inside the container).
Solution:
- By using remote debug we can set a breakpoint when the application running inside container
- As for the log, can “tail -f” the log file or using “docker logs -f”
Docker file after create the .jar
FROM adoptopenjdk:11-jre-hotspot as builder
ARG JAR_FILE=jar/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
FROM adoptopenjdk:11-jre-hotspot
ENV TZ="Asia/Taipei"
RUN apt-get update -y \
&& apt-get install -y less \
&& apt-get install -y tzdata \
&& apt-get install -y vim
WORKDIR /home/arta
RUN useradd arta \
&& mkdir -p log \
&& mkdir -p data \
&& chown -R arta:arta.
USER arta
EXPOSE 8080
ENTRYPOINT ["./entrypoint.sh"]with the example of “entrypoint.sh”
#!/bin/bash
exec java \
-cp \
/home/arta/BOOT-INF/classes:/home/arta/BOOT-INF/lib/* \
com.project.web.demo_web.DemoWebApplicationNow for the docker compose, the important part is to setup the “ports” and “environment” to allow remote debugging in port “5005” (or any port that you want)
version: "3.8"
services:
web:
build: docker/web
image: demo-web
restart: always
ports:
- "8080:8080"
- "5005:5005"
volumes:
- web-log:/home/arta/log
- web-data:/home/arta/data
environment:
- SPRING_PROFILES_ACTIVE=${SPRING_ENV?err}
- JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
volumes:
web-data:
external:
name: demo_web-data-${SPRING_ENV?err}
web-log:This is the part that allow remote debugging in Java app.
- JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005In VScode, find “launch.json” and you will see something similar to this, then add the “attach” configuration, adjust the other parameter such as ipAddress of the hostName and the port, then save the file.
Get Agung Tri Arta Hariyanto’s stories in your inbox
Join Medium for free to get updates from this writer.
There is multiple mode for “request:” parameter such as launch, attach, remote, build, test. Its depends to what kind of request that you want to perform to the java app
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Attach to local container web",
"request": "attach",
"hostName": "10.10.172.119",
"port": "5005"
}]
}Then on the debug icon in Vscode and select the newly created configuration. Start your app
Make sure you start the docker compose, then try to attach and remotly debug running Spring Boot app within the container. Attempt to set a breakpoint in the VScode to see whether the process of the Java app is halted.
That’s it! now you can remote debug and put breakpoint in your java app inside the container. Thanks for reading.





