Skip to content

Instantly share code, notes, and snippets.

@Ragdata
Last active May 5, 2023 04:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ragdata/8c3d960b9a9b330bc9906a71dec043ac to your computer and use it in GitHub Desktop.
Save Ragdata/8c3d960b9a9b330bc9906a71dec043ac to your computer and use it in GitHub Desktop.
Docker in Windows 11 using WSL2

Setup Docker to run on Windows 11 & Ubuntu WSL2 AND still be able to use Docker Desktop to monitor the lot!!

The goal here is to set up a container cluster using WSL2, Ubuntu Linux, and other Open Source packages. The secondary goal being to be able to use it from both Linux and Windows AND to be able to still use Docker Desktop under either Windows or Linux to manage things.

To do so, we need to setup WSL2, Ubuntu, dockerd and containterd, then build docker-cli for Windows and finally wire them all together so they can talk to each other.

Prerequisites

First you need to set up Windows so that you can use virtualised containers, as well as WSL2. Type the following in a terminal:

Enable-WindowsOptionalFeature -Online -FeatureName 'Containers' -All
Enable-WindowsOptionalFeature -Online -FeatureName 'Microsoft-Hyper-V' -All
Enable-WindowsOptionalFeature -Online -FeatureName 'VirtualMachinePlatform' -All
Enable-WindowsOptionalFeature -Online -FeatureName 'Microsoft-Windows-Subsystem-Linux' -All

winget

To install software, we'll use winget - the package manager for Windows. This is the easiest way to install stuff on Windows these days and ensure it remains up to date.


Windows Terminal

We'll be using the console a lot - so if you're not already using Windows Terminal, it's time you started doing so. You can get it from the Microsoft Store.

Note: Windows Terminal requires Windows 10 1903 (build 18362) or later


Powershell

Now that we have the basics up and running, you should install Powershell if you don't already have it:

winget install --id 'Microsoft.Powershell' --scope machine

Ubuntu Linux

If you don't already have a copy of Ubuntu Linux installed under WSL2, type the following command in your console:

winget install Canonical.Ubuntu

If you DO already have a copy of Ubuntu Linux inxtalled under WSL2, type the following command to make sure you're up-to-date:

wsl.exe --update

Once it's installed, type:

ubuntu

You will see the message:

Installing, this may take a few minutes

Once it finishes, you must create a default account. You can use whatever username you like; I usually use my moniker 'Ragdata' and a password which is relatively easy to remember.


Install GUI Support

NOTE: You will only be able to install GUI Support for Ubuntu on WSL2 if you can find your graphics card listed HERE

  1. Install GCC if you haven't already:
sudo apt install gcc -y
  1. Install CUDA-Keyring
sudo wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
  1. Update & Install CUDA
sudo apt update
sudo apt install cuda -y
  1. Install basic X11 applications:
sudo apt install x11-apps

Install Docker in WSL

Open a terminal window to WSL Ubuntu and start by upgrading everything that can be:

sudo apt update && sudo apt upgrade -y

Install Prerequisites

  1. Install packages to allow apt to use a repository over https:
sudo apt install ca-certificates curl gnupg2 lsb-release -y
  1. Add Docker's official GPG key:
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  1. Use the following command to set up the repository:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Install Docker Engine

  1. Update the apt package index and install the latest version of Docker Engine, containerd, and Docker Compose:
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y

Create Additional Non-Root User

Because docker needs to grant some hefty privileges to a non-root user, it's a good idea to set up one specifically for this task:

sudo adduser dockeruser

Then you need to grant that user Sudo privileges:

sudo usermod -aG sudo dockeruser

Finally, add this user to the docker group:

sudo usermod -aG docker dockeruser

Create a config file for the dockerd daemon:

sudo mkdir /etc/docker/
sudo pico /etc/docker/daemon.json

Add the following contents:

{
    "hosts": [ "tcp://0.0.0.0:" ],
    "tls": false
}

You can launch dockerd directly using the following command:

sudo dockerd

and stop it with CTRL-C.

If you want to launch dockerd in the background, use:

sudo dockerd &

and stop it with:

sudo pkill dockerd

Install Docker Desktop

If you haven't already installed it, install Docker Desktop under windows:

winget install -e --id Docker.DockerDesktop

Configure Docker Desktop

  1. Open Docker Desktop and click on Settings >> General and check the box next to "Use the WSL2 based Engine"

  2. Still under Settings >> General, ensure that the last option in the list "Use Docker Compose V2" is checked

  3. Still under Settings, click Resources >> WSL Integration; ensure that "Enable integration with my default WSL distro" is checked, and also make sure that you select your preferred default from the list below that option (should be Ubuntu-20.04)

  4. Verify that Docker Engine is installed correctly:

sudo service docker start
sudo docker run hello-world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment