4

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?

Haenela
  • 49
  • 3
  • Why not use Spring Tools Suite? https://spring.io/tools – Sully Nov 21 '19 at 11:28
  • please check https://stackoverflow.com/questions/44372595/how-do-attach-to-a-remote-java-debugger-using-visual-studio-code – ozkanpakdil Nov 21 '19 at 16:02
  • @Sully I have the extension installed but what should it do? – Flimtix Apr 14 '22 at 10:33
  • Check https://stackoverflow.com/questions/58995110/remote-debug-spring-boot-application and https://www.jetbrains.com/help/idea/run-and-debug-a-spring-boot-application-using-docker-compose.html#eb1532a2 and https://www.ibm.com/cloud/blog/four-steps-to-debugging-java-spring-boot-microservices-running-in-docker-containers – Sully Apr 14 '22 at 15:54

1 Answers1

2

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

Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69