Go Dev Container
Ultimate Go development container for Visual Studio Code








Features
- Compatible with
amd64, 386, arm64, armv6 and armv7 CPUs
ghcr.io/qdm12/godevcontainer:alpine and ghcr.io/qdm12/godevcontainer
- Based on Alpine 3.24 (size of 1.21GB)
ghcr.io/qdm12/godevcontainer:debian
- Based on Debian Trixie Slim (size of 1.37GB)
- Based on qmcgaw/basedevcontainer
- Based on either Alpine or Debian
- Minimal custom terminal and packages
- See more features
- Go 1.27 code obtained from the latest tagged Golang Docker image
- Go tooling integrating with VS code, all cross built statically from source at the binpot:
- Terminal Go tools
- Runs as an unprivileged user (
user, uid/gid 1000 by default, configurable), see Non-root user
apk (Alpine) and apt-get (Debian) install packages from that user, no sudo involved
- Cross platform
- Easily bind mount your SSH keys to use with git
- Manage your host Docker from within the dev container, more details at qmcgaw/basedevcontainer
- Compatible with both the Docker and Podman engines
- Extensible with docker-compose.yml
- Comes with extra Go binary tools for a few extra MBs:
kubectl, kubectx, kubens, stern and helm
Requirements
Setup for a project
- Setup your configuration files
-
With style 💯
docker run -it --rm -v "/yourrepopath:/repository" qmcgaw/devtainr:v0.2.0 -dev go -path /repository -name projectname
Or use the built binary
-
Or manually: download this repository and put the .devcontainer directory in your project. It holds a docker/ and a podman/ configuration, pick the one matching your container engine.
- If you have a .vscode/settings.json, eventually move the settings to
.devcontainer/*/devcontainer.json in the "settings" section as .vscode/settings.json take precedence over the settings defined in .devcontainer//devcontainer.json*.
- Open the command palette in Visual Studio Code (CTRL+SHIFT+P) and select
Dev Containers: Open Folder in Container... and choose your project directory
Non-root user
The image runs using an unprivileged account user, created by the base image.
💁 In case you need root access, see the root user section
The non root user is configurable with container build arguments, defined both in the Dockerfiles and in the devcontainer.json files:
| Build argument |
Default |
Description |
USERNAME |
user |
account name, home is /home/$USERNAME. Update the "remoteUser" key and mount targets in devcontainer.json to match this one if changed. |
GROUP_NAME |
user |
name of the account’s primary group |
USER_UID |
1000 |
user ID of the account |
USER_GID |
1000 |
group ID of the account’s primary group ($GROUP_NAME) |
DOCKER_GID |
|
create a docker group with this GID and put the account in it, to reach a rootful engine’s socket, see Container engine |
The unprivileged account:
- owns its home,
/go (its GOPATH), and the state directories of the package manager, so it can install and remove packages without sudo or doas
- can write to a directory a package may add or remove a file in (
/usr/local/bin, /etc/ssl/certs, /var/log…), which all belong to $GROUP_NAME and are group-writable.
- can only read and execute files installed in the container image and setup by the container engine
- cannot read
/root and /etc/shadow
On system package management, note:
- it cannot replace a file the image already has installed, since those keep their root ownership
- for the Debian container, some packages have maintainer scripts that insist on running as root, and will fail to install (i.e.
dbus); the fix is to finish the job as root for example docker exec --user root <container-name> sh -c 'apt-get install -y dbus && dpkg --configure -a'
Root user
If you need to run a command as root, exec in the container using for example
docker exec --user root <container-name> command
Root has the same terminal setup as the unprivileged account, given its configuration files are symlinks into /home/user.
Note however that by default the zsh history and ssh files are not symlinked or copied from the unprivileged user to root.
The shell prints Running as root at startup, so you always know which account a terminal belongs to.
Customization
See the .devcontainer/README.md document in your repository.
When you extend the image in a Dockerfile, the build runs as the unprivileged account: use USER root before instructions that do need root, and USER user to give it back.
docker-compose.yml
You can create a .devcontainer/docker-compose.yml to create the devcontainer and other containers at the same time:
services:
vscode: # configured in devcontainer.json
build: .
postgres: # example of a database container
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: password
And change the following in your relevant devcontainer.json:
- remove
"build": { "dockerfile": "../Dockerfile" },
- add
"dockerComposeFile": "../docker-compose.yml",
- add
"overrideCommand": true,
- add
"service": "vscode",
- add
"runServices": ["vscode", "postgres"],
Finally, open the command palette in Visual Studio Code (CTRL+SHIFT+P) and select Dev-Containers: Rebuild Container
TODOs
License
This repository is under an MIT license unless indicated otherwise.
评论