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" }
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...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With