6

I have a Java application (.tar) mounted to a container. The entrypoint of the container starts that application.

Dockerfile (the backend folder is mounted into the image as a volume)

FROM openjdk:11.0.7

ENTRYPOINT /backend/entrypoint.sh

entrypoint.sh

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -Xmx2048M -jar backend.jar

Now I want to debug that running application using VSCode's debugger. According to the official VSCode documentation (blog: inspecting containers) this can easily be done with the command palette and the command Debugger: attach to Node.js process.

But in their example they use a Node.js server. In my container however, there is no Node.js process that I could attach the debugger to and I can't find an appropriate command for a Java Spring application. So how can I attach the Java debugger of VSCode to an Java application which is already running inside a Docker container?

At another place in their documentation (containers: debug common) they state the following:

The Docker extension currently supports debugging Node.js, Python, and .NET Core applications within Docker containers.

So no mention of Java there but then again at another place (remote: debugging in a container) they clearly talk about a Java application:

For example, adding this to .devcontainer/devcontainer.json will set the Java home path: "settings": { "java.home": "/docker-java-home" }

3
  • copy the tar file to the container and then untar it using the startup script Commented Oct 21, 2020 at 12:19
  • @Ashok how does this help me to debug the app? Commented Oct 21, 2020 at 12:31
  • basically, your requirement is that you want to execute java inside your container? Commented Oct 21, 2020 at 12:35

2 Answers 2

10

I got a Spring Boot app to run in an openjdk:11-jre-slim container and was able to successfully debug it with the following configuration.

First, set jvm args when running your container. You do this via entrypoint.sh but I decided to override my container entrypoint in docker-compose. I also expose the debug port.

ports: 
  - 5005:5005
entrypoint: ["java","-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=n","-jar","app.jar"]

Then add this configuration to your launch.json in vscode:

{
    "type": "java",
    "name": "Debug (Attach)",
    "projectName": "MyProjectName",
    "request": "attach",
    "hostName": "127.0.0.1",
    "port": 5005
}

You can now start your container and select "Debug (Attach)" under RUN AND DEBUG in VScode. This will begin your typical debug session with breakpoints, variables, etc...

Sign up to request clarification or add additional context in comments.

Comments

2

If you set up your run command like this

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 -jar App.jar 

(or however you like to call it, the important bit are the options)

Then make your docker container expose that port. I usually use a docker compose file to do that, so you can easily map the port however you like at run time

4 Comments

Yes, that's exactly how my setup is (I updated my question). But how can I then attach the VSCode Java debugger to that JVM?
Oh OK, there is the extension "debugger for java" that supports attaching.
And how do I do that?
Left click on the plugins toolbar (looks like a jigsaw) then search for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.