Configuring Delve Debugger in a Go DevContainer
Delve is Go's debugger, and running it inside the devcontainer lets you set breakpoints against the real container runtime. This page configures Delve for both launching a program and attaching to a running Go service, with the source mapping right.
Prerequisites
You need a Go devcontainer with the Go extension and Delve.
- A Go Feature pinned in the container.
- The Go extension (installs Delve) in the container.
- A debug build so symbols are present.
Step-by-Step Implementation
- Ensure the Go extension (and Delve) are in the container.
{ "customizations": { "vscode": { "extensions": ["golang.go"] } }, "remoteUser": "vscode" }
-
Launch a program under the debugger (the extension provides a launch config).
-
Or run a service headless with Delve and attach.
dlv debug --headless --listen=:2345 --api-version=2 ./cmd/app
- Attach the editor and set breakpoints.
{ "name": "attach", "type": "go", "request": "attach", "mode": "remote", "port": 2345 }
Common Pitfalls
Delve issues are an optimized build or missing headless server for services.
| Symptom | Root Cause | Remediation |
|---|---|---|
| Breakpoints don't bind | Optimized build, symbols stripped | Build without -s -w; keep debug info |
| Can't attach to a service | No headless Delve server | Run dlv --headless --listen |
| Attach refused | Port not forwarded | Forward the Delve port |
| Wrong line mapping | Stale build vs source | Rebuild so the binary matches sources |
Conclusion
Debug Go where it runs: put Delve in the container via the Go extension, and either launch under it or run it headless and attach to a service. Build with debug info so breakpoints bind, and you're debugging the exact binary the container executes.
FAQ
Why won't my Go breakpoints bind?
Usually the binary was built optimized with symbols stripped (-ldflags "-s -w"), so Delve has nothing to map breakpoints to. Build with debug information (the default debug build) so symbols are present, and ensure Delve runs in the container against that binary.
How do I debug a running Go service in the container?
Start it under Delve in headless mode (dlv debug --headless --listen=:2345 --api-version=2), forward the port, and use a remote attach configuration. The editor connects to the live service and you set breakpoints without restarting it under the debugger.
Does Delve run on the host or in the container? In the container — the Go extension installs Delve into the container, so it debugs the container's binary against the container's Go toolchain. That keeps the debug session faithful to what actually runs, including any container-specific build tags or environment.
Related
- Up to Go Development Environment with gopls & Modules — the Go overview.
- Caching Go Module Downloads with a Named Volume — fast builds before debugging.
- Debugging Rust Async Code in VS Code Containers — a sibling in-container debugging guide.