Debugging Java in a DevContainer
You need to set breakpoints in a Java service that runs inside a devcontainer. This page attaches the VS Code Java debugger to the in-container JVM — launching directly, or attaching over JDWP for a running service — so debugging works against the real container runtime.
Prerequisites
You need a Java devcontainer with the debugger extension.
- A Java devcontainer with a pinned JDK.
- The Java extension pack (includes the debugger) in the container.
- A JDWP port if attaching to a running service.
Step-by-Step Implementation
- Ensure the Java pack is installed in the container.
{ "customizations": { "vscode": { "extensions": ["vscjava.vscode-java-pack"] } }, "remoteUser": "vscode" }
-
Launch the app under the debugger (simple case) — the extension provides a launch config.
-
Or start a service with JDWP and attach.
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -jar app.jar
- Attach the editor to the JDWP port and set breakpoints.
{ "type": "java", "request": "attach", "hostName": "localhost", "port": 5005 }
Common Pitfalls
Java debug issues are a host-run debugger or a missing JDWP port.
| Symptom | Root Cause | Remediation |
|---|---|---|
| Breakpoints don't bind | Debugger not in the container | Install the Java pack in the container |
| Can't attach to a service | No JDWP agent | Start the JVM with the JDWP agent |
| Attach refused | JDWP port not forwarded | Forward the JDWP port to the host |
| Wrong source mapping | Build output stale | Rebuild so class files match sources |
Conclusion
Debug Java where it runs: install the Java debugger into the container and either launch under it or attach over JDWP to a running service. Breakpoints then bind against the real in-container JVM, so what you debug is what actually executes.
FAQ
Why won't my Java breakpoints bind?
Usually because the debugger is running on the host rather than in the container. Install the Java extension pack into the container (via customizations.vscode.extensions) so the debugger runs on the server against the in-container JVM, and breakpoints bind to the real runtime.
How do I debug a long-running service in the container?
Start the JVM with the JDWP agent (-agentlib:jdwp=...,address=*:5005), forward that port, and use an attach launch configuration pointing at it. The editor then attaches to the live service and you can set breakpoints without restarting it under the debugger.
Do I need to forward the JDWP port? If you attach from the host editor to a container JVM, yes — forward the JDWP port (e.g. 5005) so the debugger can reach it. When the whole debug session runs in-container via the server, the port stays internal and no forwarding is needed.
Related
- Up to Java & JVM DevContainer Configuration — the overview of JVM setup.
- Configuring the JDK Version in a DevContainer — the JDK you debug against.
- Caching Maven and Gradle Dependencies in a DevContainer — fast builds before debugging.