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.
|
|
devcontainer.json
Create the devcontainer.json
file inside .devcontainer
folder and add the following content.
|
|
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.