|--- .vscode
| |--- launch.json
|
|--- Dockerfile
|--- Dockerfile.base
|--- Dockerfile.debug
|--- main.go
|--- README.md
|--- request.http
- type
docker run --rm -it -v ${pwd}:/go/src/app -w /go/src/app -p 3000:3000 golang:1.12
- to check whether you have bind the volume correctly or not, type
ls
- you should see your local project files inside there
- run
go run main.go
Explanation regarding the Docker command
Command | Explanation |
---|---|
docker run | to create a new Docker container |
--rm | delete container after exit, for sanity purpose |
-it | to enable interactive mode |
-v ${pwd}:/go/src/app | to mount bind between local directory and container directory |
-w | to change into a specific directory inside container |
-p 3000:3000 | to set inbound-outbound port |
golang:1.12 | base image |
- build the base image first,
docker image build -f Dockerfile.base -t go-base .
- On your VSCode, click
ctrl + L shift + p
- Select/write
Go: Install/Update Tools
- Search
dlv
- Click
OK
button - Set up
launch.json
file inside.vscode
folder - For debugging purpose, open up a new port to let dlv listen for changes. i.e.
:1234
- Create a Dockerfile specifically for debugging purpose. i.e.
Dockerfile.debug
- Build it,
docker image build -f Dockerfile.debug -t go-debug .
- Run it,
docker run --rm -d -it -v ${pwd}:/go/src/app -w /go/src/app -p 3000:3000 -p 1234:1234 --security-opt=seccomp:unconfined go-debug:latest
- Run your debugger and start to place your breakpoint
Explanation regarding the Docker command
Command | Explanation |
---|---|
-d | detach, so you will not be directed into the container's CLI |
-p 1234:1234 | open up another port for debugging purpose |
--security-opt=seccomp:unconfined | in order to let dlv run into your container, you need to allow it by without using default secure computing mode (seccomp) profile |
Additional explanation about Debug step
- To know more about dlv CLI command, check here
- To know more about VSCode debugging, especially in Go, check here
- Create the base image on
Dockerfile.base
. The purpose of this Dockerfile is to build the standard environment that could be applicable for debug and production - Create the debugging-purpose image on
Dockerfile.debug
. We do not need to have unrelated packages for production, such asgo-delve
(for debugging purpose) and exposing a new port, which may harm your container. - Create the production-purpose image on
Dockerfile
. We set the standard naming style for production.