Docker containers are a popular way to create reproducible development environments without having to install complex dependencies on your local machine. This also allows new team members to reproduce your environment by installing docker and opening your workspace in Visual Studio Code.
So, in this little article, I will guide you to setup a dev container for Golang.
Dependencies
Docker
For more details of Docker, please check Get Started with Docker
Remote development extension pack
The Remote Development extension pack allows you to open any folder in a container or or a remote machine and take advantage of VS Code’s full feature set.
Dev Container configuration
Create a .devcontainer
folder at the root of your project.
├── .devcontainer
│ ├── Dockerfile
│ └── devcontainer.json
├── go.mod
└── main.go
Dockerfile
Create the Dockerfile
inside .devcontainer
folder and add the following content.
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.166.1/containers/go/.devcontainer/base.Dockerfile
# [Choice] Go version: 1, 1.16, 1.15
ARG VARIANT="1"
FROM mcr.microsoft.com/vscode/devcontainers/go:0-${VARIANT}
# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>
# [Optional] Uncomment the next line to use go get to install anything else you need
# RUN go get -x <your-dependency-or-tool>
devcontainer.json
Create the devcontainer.json
file inside .devcontainer
folder and add the following content.
{
"name": "iris-grpc-api",
"build": {
"dockerfile": "Dockerfile",
"args": {
// Update the VARIANT arg to pick a version of Go: 1, 1.16, 1.15
"VARIANT": "1.16",
}
},
"runArgs": [
"--cap-add=SYS_PTRACE",
"--security-opt",
"seccomp=unconfined"
],
"settings": {
"terminal.integrated.defaultProfile.linux": "/bin/bash",
"go.toolsManagement.checkForUpdates": "local",
"go.useLanguageServer": true,
"go.gopath": "/go",
"go.goroot": "/usr/local/go"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"golang.Go",
"mhutchie.git-graph"
],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "go version",
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
The devcontainer.json
file tells VS Code how to access or create a development container. With this configuration, VS Code will:
- Create a Docker image with the variant specified, for more details about config options, please check Go Containers.
- Install
golang.Go
extension that provides rich language support for the Go programming language. - Install
mhutchie.git-graph
extension that provides a Git Graph View of your repository.
For more details about Dev Container config options, please check Dev Container JSON Reference.
Start the Development Container
Launch the VS Code Command Panel(ctrl/cmd + Shit + P) and look for the command Remote-Containers: Reopen in container
.
This will build the Docker image defined inside .devcontainer/Dockerfile
, install the VS Code dependencies in there and bind mount everything for you.
Finally, you have a terminal running inside VS Code(open a terminal if you don’t see it).
Wrapping up
I hope you enjoyed this little guide. Remember you can add additional OS packages in your Dockerfile.