In April 2019, Visual Studio Code released a new extension Remote Containers Development which allows you to code with your development environment in a Docker container. Learn more.
In your project:
.devcontainer
.devcontainer/Dockerfile
.devcontainer/devcontainer.json
Since Visual Studio Code 1.38 release in August 2019, there is now support for Alpine based dev containers, which is great for size as Alpine is about 5MB!
For Go, I decided to use the language server gopls only and it has been working great so far and bundles all the required tools.
I also wanted a shell based on zsh and oh-my-zsh to feel nice developing in Visual Studio Code.
Finally, I have mapped my SSH keys so that I can interact with git from inside a terminal window in Visual Studio code.
Here comes the Dockerfile:
ARG GO_VERSION=1.13 ARG ALPINE_VERSION=3.10FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION}
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=1000Setup user
RUN adduser $USERNAME -s /bin/sh -D -u $USER_UID $USER_GID &&
mkdir -p /etc/sudoers.d &&
echo $USERNAME ALL=(root) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME &&
chmod 0440 /etc/sudoers.d/$USERNAMEInstall packages and Go language server
RUN apk add -q --update --progress --no-cache git sudo openssh-client zsh
RUN go get -u -v golang.org/x/tools/cmd/gopls 2>&1Setup shell
USER $USERNAME
RUN sh -c “$(wget -O- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)” “” --unattended &> /dev/null
ENV ENV=“/home/$USERNAME/.ashrc”
ZSH=/home/$USERNAME/.oh-my-zsh
EDITOR=vi
LANG=en_US.UTF-8
RUN printf ‘ZSH_THEME=“robbyrussell”\nENABLE_CORRECTION=“false”\nplugins=(git copyfile extract colorize dotenv encode64 golang)\nsource $ZSH/oh-my-zsh.sh’ > “/home/$USERNAME/.zshrc”
RUN echo “execwhich zsh
” > “/home/$USERNAME/.ashrc”
USER root
Dockerfile
An example configuration is the following
{
“name”: “Your project Dev”,
“dockerFile”: “Dockerfile”,
// “appPort”: 8000,
“extensions”: [
“ms-vscode.go”,
“davidanson.vscode-markdownlint”,
“shardulm94.trailing-spaces”,
“IBM.output-colorizer”
],
“settings”: {
“go.useLanguageServer”: true
},
“postCreateCommand”: “go mod download”,
“runArgs”: [
“-u”,
“vscode”,
“–cap-add=SYS_PTRACE”,
“–security-opt”,
“seccomp=unconfined”,
// map SSH keys for Git
“-v”, “${env:HOME}/.ssh:/home/vscode/.ssh:ro”
]
}
This installs a few extensions in your container and bind mounts in read only your .ssh
directory for Git purposes.
Side note: VS code is clever enough to ignore comments in devcontainer.json
It also runs go mod download
after the container is setup to download your Go dependencies when the container is ready.
Launch it by opening the VS code command palette and select Remote-Containers: Open folder in container…
and choose the current folder.
This will build the Docker image, install the VS code dependencies in there and bind mount everything for you!
Finally, you have a terminal running a beautiful zsh inside VS code (open a terminal if you don’t see it).
Just change the Dockerfile to FROM qmcgaw/godevcontainer
. Yep, that’s it .
It will just pull the image from Docker Hub, corresponding to my Github repo… Plus I should keep it to the top with new features
I hope you enjoyed this little guide. It took me a few hours to figure it all out so I hope it can save you some time to enjoy a beer instead.
Originally published by Quentin McGaw at medium.com
======================================
Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter
5 Quick and Easy Data Visualizations in Python with Code
Visual Studio Code Settings and Extensions for Faster JavaScript Development
#go #docker #visual-studio #web-development