Skip to content

Instantly share code, notes, and snippets.

@bkonetzny
Last active November 5, 2023 13:28
Show Gist options
  • Save bkonetzny/b769afcab375dedae743ac648622a4f1 to your computer and use it in GitHub Desktop.
Save bkonetzny/b769afcab375dedae743ac648622a4f1 to your computer and use it in GitHub Desktop.

Windows 10 (20H1 / 2004 / Mai 2020 Update) with WSL2 is out, this guide should be obsolete now.

Upgrading an existing WSL1 setup to WSL2

Docker in WSL1

Note, this is only needed on Windows 10 Home and WSL1. If you have Windows 10 Pro, you can run Docker for Windows (https://docs.docker.com/docker-for-windows).

After completing this guide, you will have a Windows 10 Home running the docker deamon via Docker Toolbox / VirtualBox. The WSL guest OS will connect to this docker deamon, and will have the Windows OS filesystem mapped as C:\ -> /c/ instead of C:\ -> /mnt/c/, which will fix path translation between docker deamon on Windows and docker client in WSL guest OS.

Setup Windows 10

Install Docker Toolbox on Windows 10: (https://github.com/docker/toolbox/releases)

Enable WSL and make sure Hyper-V is disabled via Command:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
bcdedit /set hypervisorlaunchtype off

Install an WSL guest OS via Microsoft Store:

Setup WSL guest OS

#!/bin/bash

WINDOWS_USER_DIRECTORY=/c/Users/MyUserName
DOCKER_HOST=192.168.99.100:2376
PATH_TO_ID_RSA="${WINDOWS_USER_DIRECTORY}/.ssh/id_rsa"

echo "Configuring WSL automount to root (needs Windows restart to work)"
sudo bash -c 'cat << EOF > /etc/wsl.conf
[automount]
root = /
options = "metadata"
EOF'

echo "Installing docker, docker-compose and utils for docker login"
sudo apt-get -y update \
  && apt-get -y install \
     docker.io \
     docker-compose \
     gnupg2 pass \
  && apt-get -y autoremove

echo "Exporting environments"
echo "export DOCKER_CERT_PATH='${WINDOWS_USER_DIRECTORY}/.docker/machine/certs'" >> ~/.bashrc
echo "export DOCKER_HOST=tcp://${DOCKER_HOST}" >> ~/.bashrc
echo "export DOCKER_TLS_VERIFY=1" >> ~/.bashrc
echo "export COMPOSE_TLS_VERSION=TLSv1_2" >> ~/.bashrc
source ~/.bashrc

echo "Testing docker setup"
docker info
docker-compose ps

echo "Adding id_rsa from host user"
eval $(ssh-agent)
ssh-add ${PATH_TO_ID_RSA}

Sources

Upgrade to WSL2 from existing WSL1 setup

Enable WSL2

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Download updated WSL2 kernel and Docker Desktop

You can remove Docker Toolbox (including Oracle VirtualBox) now from your system.

Set WSL version to version 2 for new and existing installations.

wsl --set-default-version 2
wsl --set-version Ubuntu-18.04 2
@marcelomaia
Copy link

great! this guide for wsl1 saved me!

wsl2 does not work on corporate proxy.

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