Skip to content

Instantly share code, notes, and snippets.

@efimovalex
Last active January 1, 2023 09:23
Show Gist options
  • Save efimovalex/91ba366acaf9dd1819e5f35f950fd544 to your computer and use it in GitHub Desktop.
Save efimovalex/91ba366acaf9dd1819e5f35f950fd544 to your computer and use it in GitHub Desktop.
Install Go & Docker under WSL.

Dev ENV setup

Docker

Download setup for Docker CE Windows.

Install it.

Go to seetings and check expose daemon.

docker

WSL setup

Open Powershell and run :

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Restart when prompted.

Open the Microsoft Store and choose your favorite Linux distribution.

windows store

Choose the distro you want and click get

windows store

Open WSL Bash terminal (Start menu -> search for "bash"), or Press Win+R, paste C:\Windows\System32\bash.exe, then click ok

Install docker for linux:

https://docs.docker.com/engine/install/

Configure WSL 1 to Connect to Docker for Windows

If you are using WSL 2 you can skip this.

Connect to a remote Docker daemon with this 1 liner:

echo "export DOCKER_HOST=tcp://localhost:2375" >> ~/.bashrc && source ~/.bashrc

Verify Everything Works

You should get a bunch of output about your Docker daemon. If you get a permission denied error, close + open your terminal and try again.

docker info

You should get back your Docker Compose version.

docker-compose --version

Bind custom mount points to fix Docker for Windows and WSL differences:

sudo mkdir /c
sudo mount --bind /mnt/c /c

sudo mkdir /d
sudo mount --bind /mnt/d /d

This is made so that docker can translate linux paths to windows paths. (/c/path/to/dir to C:/path/to/dir), otherwise the mount points in wsl will be /mnt/c/ and docker will not find MNT:/c

Fix umask issue

Set umask to 0022

umask 022 >> ~/.profile 

Configure WSL to work flawlessly with Docker

Edit /etc/wsl.conf

sudo nano /etc/wsl.conf

Contents should be:

[automount]
enabled = true
root = /
options = "uid=1000,gid=1000,umask=0022,metadata"

Windows go env setup

Download desired version from the download page.

Run the setup.

Create folders D:\work\go\ D:\work\go\win\bin D:\work\go\src

Open "Control pannel", go to "System" then "Advanced system settings", then click on the "Environment Variables" button on the bottom of the window and add these new variables to the your user.

GOPATH=D:\work\go\

GOBIN=D:\work\go\win\bin

Optional you can add you windows gobin to the windows PATH variable.

WSL go env setup

Go to go download page to get the link to the latest go version.

Download desired verison with wget:

wget https://dl.google.com/go/go1.13.6.linux-amd64.tar.gz

Install under /usr/local:

tar -C /usr/local -xzf go1.13.6.linux-amd64.tar.gz

Export vars in ~/.profile:

mkdir /d/work/go/lin/bin

echo "export GOPATH=/d/work/go" >> ~/.profile
echo "export GOBIN=/d/work/go/linux/bin" >> ~/.profile
echo "export GO111MODULE=on" >> ~/.profile
echo "export GOPROXY=direct" >> ~/.profile
echo "export GOSUMDB=off" >> ~/.profile
echo "export PATH=$PATH:/usr/local/go/bin:$GOBIN" >> ~/.profile 
source ~/.profile

Add git completion

cd ~
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
. ~/git-completion.bash >> ~/.bashrc

IDE

VS Code

Download Visual Studio Code and install it.

Open VS Code.

Press Ctrl+Shift+P in VS Code and type in Extensions: Install Extensions which will open a pannel on the right side. Search for Go Extension

go extension

Install it and reload the window.

Press Ctrl+, in VS Code , this will open the settings, and search for Shell: Windows and in the first field replace C:\Windows\System32\cmd.exe with C:\windows\sysnative\bash.exe

Press Ctrl+Shift+P in VS Code and type in Go: Install/Update Tools wait for VS Code to install necessary go tools.

Warning: Do not install VS Code for WSL Extension because it will cause issues with docker created files. VS Code will use WSL file update events to update files, which will give false files when creating them from docker, which cannot be deleted or overwritten, and they will give you headaches.

Now you should be good to GO...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment