Docker on WSL2-Ubuntu

araguaci

araguaci

Posted on March 27, 2024

Docker on WSL2-Ubuntu

The Windows Subsystem for Linux v2 (WSL2) is available in preview for Windows 10 users. WSL2 is a substantial improvement over WSL and offers significantly faster file system performance and full system call capabilities. Which means we can finally run dockerd in WSL!

WSL2 is currently only available as a preview feature through the Windows Insider program. WSL2 requires Windows 10 build 18917 or higher which currently requires the "Fast Ring" of Windows Insider.

If you're not ready to run a fast ring preview on your dev box, you can download an ISO and experiment with WSL2 in a VM. Windows Insider ISOs are available here:

Setup WSL2

Setup instructions for WSL2 are available here: Make sure to install the Ubuntu distro from the Microsoft Store

Once setup, start a command prompt and run the following command to verify Ubuntu is set to version 2

# Set WSL to default to v2
wsl --set-default-version 2

# check the version
wsl -l -v

# Output should show Ubuntu and version 2
# if not, you can upgrade the distro
# this usually takes 5-10 minutes
wsl --set-version Ubuntu 2
Enter fullscreen mode Exit fullscreen mode

Install Docker

From WSL bash, run the following commands to setup Docker. These are nearly identical to setting up on an Ubuntu VM.

# update the package manager and install some prerequisites (all of these aren't technically required)
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common libssl-dev libffi-dev git wget nano

# create a group named docker and add yourself to it
#   so that we don't have to type sudo docker every time
#   note you will need to logout and login before this takes affect (which we do later)
sudo groupadd docker
sudo usermod -aG docker ${USER}

# add Docker key and repo
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

# (optional) add kubectl key and repo
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

# update the package manager with the new repos
sudo apt-get update

# upgrade the distro
sudo apt-get upgrade -y
sudo apt-get autoremove -y

# install docker
sudo apt-get install -y docker-ce containerd.io

# (optional) install kubectl
sudo apt-get install -y kubectl

# (optional) install latest version of docker compose
sudo curl -sSL https://github.com/docker/compose/releases/download/`curl -s https://github.com/docker/compose/tags | \
grep "compose/releases/tag" | sed -r 's|.*([0-9]+\.[0-9]+\.[0-9]+).*|\1|p' | head -n 1`/docker-compose-`uname -s`-`uname -m` \
-o /usr/local/bin/docker-compose && sudo chmod +x /usr/local/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode

Finalizing setup

Currently, Docker doesn't auto start, so you have to start the service each time. The below command will patch your .profile to start Docker each time you login.

echo "sudo service docker start" >> ~/.profile

# exit and then restart WSL
exit
Enter fullscreen mode Exit fullscreen mode

Once you exit and restart WSL (just run "wsl" from the command prompt or Windows Run command), Docker should work correctly. To test, run the following command from WSL bash.

docker run -it hello-world
Enter fullscreen mode Exit fullscreen mode

Lagniappe

I prefer WSL to always start in my home directory (~), so I use the following to add that to my .profile

echo "cd ~" >> ~/.profile
Enter fullscreen mode Exit fullscreen mode

Install Azure CLI

# add Azure CLI key and repo
curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc.gpg
CLI_REPO=$(lsb_release -cs)
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ ${CLI_REPO} main" | sudo tee /etc/apt/sources.list.d/azure-cli.list

# update the package manager
sudo apt-get update

# install Azure CLI
sudo apt-get install -y azure-cli
Enter fullscreen mode Exit fullscreen mode

Install dotnet core

# add the dotnet core repo
echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-bionic-prod bionic main" | sudo tee /etc/apt/sources.list.d/dotnetdev.list

# update the package manager
sudo apt-get update

# install dotnet core
sudo apt-get install -y dotnet-sdk-2.2
Enter fullscreen mode Exit fullscreen mode
πŸ’– πŸ’ͺ πŸ™… 🚩
araguaci
araguaci

Posted on March 27, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Docker on WSL2-Ubuntu
docker Docker on WSL2-Ubuntu

March 27, 2024