Setup Docker Engine in WSL/Ubuntu Terminal

Docker in wsl 2

You can install Docker Engine in different ways, depending on your needs:

  1. Docker Engine comes bundled with . This is the easiest and quickest way to get started.
  2. Set up and install Docker Engine from .
  3.  and manage upgrades manually.
  4. Use a . Only recommended for testing and development environments.

Setting up the host machine

Are you on a Windows machine, you can use Docker Desktop or install Docker Engine in WSL 2 (make sure the WSL is version 2).

This article is for you as I will walk you through step by step how to enable your local machine for docker development using wsl 2.

To achieve this we will be using WSL2 (Windows sub-system for Linux) that’s available as a feature in Windows to enable developers to run a Linux environment.

Installing WSL2

You can following this instruction Install WSL and setup ZSH + Oh My ZSH to install WSL or foloowing below steps. You can skip this step if you had WSL2 in your machine.

Open the command prompt and execute the following commands:

Check wsl list

1wsl -l -o # or wsl --list --online
WSL List

This command is to check list of available Linux distributions available for download through the online store.

Now we install Ubuntu version.

Install Ubuntu

1wsl --install Ubuntu

Verify installation and check wsl version run this command:

Check wsl version

1wsl -l -v # or you can run wsl --list --verbose
WSL version

If the version if WSL still version 1 run this command

Set to wsl 2

1wsl --set-default-version 2 wsl --set-version Ubuntu-20.04 2

Restart WSL and restart terminal

1wsl --shutdown

Open Bash shell (Ubuntu) Windows start → search for Ubuntu. You will be prompted to set your root username and password in Ubuntu.

Installing docker-engine

Once you are logged in to your Linux distro, Add user as sudo (administrator)

1grep -E 'sudo|wheel' /etc/group 2# Check 3sudo grep -E '%sudo|%wheel' /etc/sudoers

You should see: %sudo ALL=(ALL:ALL) ALL

Update your Ubuntu distro (type Y when asked)

1sudo apt-get update & sudo apt-get upgrade

Remove previous docker installation residue, if exist

1sudo apt remove docker docker-engine docker.io containerd runc

Update apt and install packages to allow apt to use a repository over HTTPS

1sudo apt-get update 2sudo apt-get install ca-certificates curl gnupg lsb-release

Add docker official GPG key

1curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Set up a stable repository

1echo \ 2 "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ 3 $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Install docker-engine using apt

1sudo apt-get update 2sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Add the user to the docker group

1sudo groupadd docker 2sudo usermod -aG docker $USER

Run docker service

1sudo service docker start

Check status (Must be “docker is running”)

1sudo service docker status