Docker on WSL2-Ubuntu
Tags: docker, ubuntu, wsl
Published: 2024-03-27 18:41:00

---
title: Docker on WSL2-Ubuntu
published: true
description: Install Docker on Windows Subsystem for Linux v2 (Ubuntu)
tags: [docker, ubuntu, wsl]
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6hi6pqbthx548se1g625.png
# Use a ratio of 100:42 for best results.
published_at: 2024-03-27 18:41 +0000
---

[](#install-docker-on-windows-subsystem-for-linux-v2-ubuntu)
-------------------------

The Windows Subsystem for Linux v2 [(WSL2)](https://docs.microsoft.com/en-us/windows/wsl/wsl2-about) 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](https://insider.windows.com/en-us/). 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:](https://www.microsoft.com/en-us/software-download/windowsinsiderpreviewadvanced)

[](#setup-wsl2)Setup WSL2
-------------------------

Setup instructions for WSL2 are available [here:](https://docs.microsoft.com/en-us/windows/wsl/wsl2-install) Make sure to install the Ubuntu distro from the [Microsoft Store](https://aka.ms/wslstore)

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
    
    

[](#install-docker)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
    
    

[](#finalizing-setup)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
    
    

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
    
    

[](#lagniappe)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
    
    

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
    
    

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