A docker image to enable golang program develop and debug(with gdb) in any OS, whether macOS or Windows, where installing gdb is rather troublesome.
-
Choosing ArchLinux(Instead of Alphine or other Debian distro) is simply because personally I like the lightweighted distro with the (almost) most efficient package manager.
-
vim is added for temporal modification of source code in the container directly.
-
git is added to avoid the git related error if you happens to enter git command in the container (Maybe I will remove it later??).
-
Other command line related tools/programs(e.g. tmux, screen .etc) are not installed in the image
To run gdb properly with this image, please append privilege param to docker run
:
Do remember to replace the /gopath/on/host
with the absolute $GOPATH
on your host.
docker run --privileged --mount type=bind,source=/gopath/on/host,target=/ws -it phye/godev:latest /bin/bash
Say that you're developing golang programs in macOS and want to debug your golang binary using gdb, here is typical steps:
- Write your golang code in macOS under your GOPATH. Let's assume that
algobase
package contains some basic component, whileplayground
directory is the main package which usesalgobase
package, and thus we have the following layout:
$ echo $GOPATH
/ws/go
$ ls $GOPATH/src/github.com/phye
algobase playground
- Now you need to debug your code in
playground
directory in macOS, all you need to do is:
$ docker run --privileged --mount type=bind,source=/ws/go,target=/ws -it phye/godev:latest /bin/bash
(container)$ cd /ws/go/src/github.com/phye/playground
(container)$ go build -gcflags "-N -l" sth.go
(container)$ gdb sth
(container gdb) break 33
(container gdb) r # Other gdb commands you like to use during debug