Haenela
Haenela

Reputation: 49

Visual studio code: Debug java spring boot applications inside docker compose

I am trying to debug my spring boot applications which are running inside a docker-compose. For one application i added an additional port and command

ports:
      - "4010:8080"
      - "5858:5858"
command: java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5858 -jar jar-file.jar

The next step should be adding a remote connection for the debugger. But here i'm unaware how to achieve this in visual studio code. Can anyone help here on achieving this? Also if it's possible to configure the debugger for all other services or is it only possible for one port?

Upvotes: 4

Views: 1602

Answers (1)

Luis Lavieri
Luis Lavieri

Reputation: 4109

Ok. I figured it out. In my case, it's a little different since I am not using a fat jar.

My Dockerfile looks like this:

# extract layers
FROM adoptopenjdk:11-jre-hotspot as builder
WORKDIR /application
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

# copy layers
FROM adoptopenjdk:11-jre-hotspot
WORKDIR /application
COPY --from=builder /application/dependencies/ ./
COPY --from=builder /application/spring-boot-loader/ ./
COPY --from=builder /application/snapshot-dependencies/ ./
COPY --from=builder /application/application/ ./

VOLUME /files
# ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
ENTRYPOINT ["java","-agentlib:jdwp=transport=dt_socket,address=*:8000,server=y,suspend=n","-Djava.security.egd=file:/dev/./urandom","org.springframework.boot.loader.JarLauncher"]

Notice the last line. It has all the features to enable the app to be remotely debugged.

Now, In the ports section of my compose I have:

ports:
  - 8080:8080
  - 8000:8000

Both ports are needed in this case. Because the debugger will be attached to the one mentioned in the Dockerfile, while the other one is the regular entry point of the app.

Now, inside .vscode/launch.json, all we need to do is point to that port.

    {
        "type": "java",
        "name": "Debug (Attach)",
        "projectName": "pdf-tools-webapi",
        "request": "attach",
        "hostName": "localhost",
        "port": 8000
    }

Then, if you hit the other port, it should stop to debug! :)

enter image description here

Upvotes: 2

Related Questions