go-container-debugging
go-container-debugging copied to clipboard
Debugging a Docker container, running a Go binary, with Delve, in Visual Studio Code.
Debugging a Go executable in a Docker Container with VS Code
This repository illustrates how to debug into a Docker container running a Go binary.
Prerequisites:
- Go
- Visual Studio Code
- Delve, the debugger for Go
- Docker and Docker Compose
In a nutshell
- Build the Go binary (for Linux) by running
make - Build the Docker container by running
docker-compose up --build - Open the code in Visual Studio by running
code . - Set some breakpoints in the code
- Start
Debug into Dockerin the Debug section of VS Code - Open
http://localhost:8080in your browser - See the breakpoints being hit
- Profit!
Details
Now, the application is a simply webserver that prints the
current time on every page refresh. By default, it runs on
http://localhost:8080.
The Dockerfile however won't start the executable. Instead,
it will install the delve debugger and start it in
headless mode. In addition to the application port 8080, it
will open a debugger port at 2345.
When starting a VS Code debugging session, it will connect to
that port 2345 and will make the debugger start the application.
So now you can hit http://localhost:8080 and breakpoints
should work as expected.
Notice that when you stop the debugger, the container will also be shut down and you might want to restart it.
References
[1] Remote debugging Go code with VS Code: https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code#remote-debugging
License
MIT.