WSL

Install Docker Engine and Docker Compose on Windows Subsystem for Linux (WSL 2)

· 5 min read
Install Docker Engine, containerd and Docker Compose on WSL 2

If you already have Docker Desktop installed or want to use docker on both Windows and WSL2, then  Docker Desktop is better choice. But I personally prefer to install and run the "most-loved Tool" docker inside Windows Subsystem for Linux (WSL 2). This keeps the development environment isolated and the Windows host tidy.

What is Docker?

"Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production." - https://docs.docker.com

Prerequisites

We need WSL 2 with version set as 2. To check WSL version set to installed Linux distro, execute wsl -l -v or wsl --list --verbose

C:\Users\AwsJunkie>wsl -l -v
  NAME           STATE           VERSION
* MyLabEnv       Stopped         2

If you have upgraded from WSL1 to WSL2 and previously installed Linux Distro is using WSL1, update the version using wsl --set-version <linux-distro-name> 2 command. For example,

C:\Users\AwsJunkie>wsl --list --verbose
  NAME           STATE           VERSION
* MyLabEnv         Stopped         1

C:\Users\AwsJunkie>wsl --set-version Ubuntu 2
Conversion in progress, this may take a few minutes...
For information on key differences with WSL 2 please visit https://aka.ms/wsl2
Conversion complete.

C:\Users\AwsJunkie>wsl --list --verbose
  NAME           STATE           VERSION
* MyLabEnv         Stopped         2

Installation Steps

Update apt package index with latest version found in configured repositories.

abhijit@AwsJunkie:~$ sudo apt update
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
:
:
Fetched 25.5 MB in 11s (2373 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
79 packages can be upgraded. Run 'apt list --upgradable' to see them.

Install prerequisite packages (ca-certificates, curl, gnupg, lsb-release)  that allows apt  to access repository over HTTPS protocol.  

abhijit@AwsJunkie:~$ sudo apt install ca-certificates curl gnupg lsb-release -y
Reading package lists...
Building dependency tree...
Reading state information...
lsb-release is already the newest version (11.1.0ubuntu4).
lsb-release set to manually installed.
gnupg is already the newest version (2.2.27-3ubuntu2.1).
gnupg set to manually installed.
The following additional packages will be installed:
  libcurl4
The following packages will be upgraded:
  ca-certificates curl libcurl4
3 upgraded, 0 newly installed, 0 to remove and 76 not upgraded.
Need to get 627 kB of archives.
After this operation, 8192 B disk space will be freed.
:
Preconfiguring packages ...
Fetched 627 kB in 1s (555 kB/s)
(Reading database ... 
:
(Reading database ... 24075 files and directories currently installed.)
Preparing to unpack .../ca-certificates_20211016ubuntu0.22.04.1_all.deb ...
Unpacking ca-certificates (20211016ubuntu0.22.04.1) over (20211016) ...
Preparing to unpack .../curl_7.81.0-1ubuntu1.7_amd64.deb ...
Unpacking curl (7.81.0-1ubuntu1.7) over (7.81.0-1ubuntu1.4) ...
Preparing to unpack .../libcurl4_7.81.0-1ubuntu1.7_amd64.deb ...
Unpacking libcurl4:amd64 (7.81.0-1ubuntu1.7) over (7.81.0-1ubuntu1.4) ...
Setting up ca-certificates (20211016ubuntu0.22.04.1) ...
Updating certificates in /etc/ssl/certs...
rehash: warning: skipping ca-certificates.crt,it does not contain exactly one certificate or CRL
0 added, 3 removed; done.
Setting up libcurl4:amd64 (7.81.0-1ubuntu1.7) ...
Setting up curl (7.81.0-1ubuntu1.7) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
Processing triggers for ca-certificates (20211016ubuntu0.22.04.1) ...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.

Add official GPG key for Docker repository.

abhijit@AwsJunkie:~$ sudo mkdir -p /etc/apt/keyrings
abhijit@AwsJunkie:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Setup repository.

abhijit@AwsJunkie:~$ 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

Update apt package index once again to pull latest from above repository.

abhijit@AwsJunkie:~$ sudo apt update

Upgrade all packages to latest version.

abhijit@AwsJunkie:~$ sudo apt upgrade -y

Install latest version of Docker Engine, Docker CLI, Containerd and Docker Compose.

abhijit@AwsJunkie:~$ sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  dbus dbus-user-session dbus-x11 docker-buildx-plugin docker-ce-rootless-extras docker-scan-plugin libdbus-1-3
  libltdl7 libslirp0 pigz slirp4netns
Suggested packages:
  aufs-tools cgroupfs-mount | cgroup-lite
The following NEW packages will be installed:
  containerd.io dbus-user-session docker-buildx-plugin docker-ce docker-ce-cli docker-ce-rootless-extras
  docker-compose-plugin docker-scan-plugin libltdl7 libslirp0 pigz slirp4netns
The following packages will be upgraded:
  dbus dbus-x11 libdbus-1-3
3 upgraded, 12 newly installed, 0 to remove and 73 not upgraded.
Need to get 111 MB of archives.
After this operation, 397 MB of additional disk space will be used.
Get:1 https://download.docker.com/linux/ubuntu jammy/stable amd64 containerd.io
:
Setting up docker-ce-rootless-extras (5:23.0.0-1~ubuntu.22.04~jammy) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...

Verify

Confirm the installation is successful.

abhijit@AwsJunkie:~$ docker version
Client: Docker Engine - Community
 Version:           23.0.0
 API version:       1.42
 Go version:        go1.19.5
 Git commit:        e92dd87
 Built:             Wed Feb  1 17:47:51 2023
 OS/Arch:           linux/amd64
 Context:           default
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
abhijit@AwsJunkie:~$ docker compose version
Docker Compose version v2.15.1

Start docker service.

abhijit@AwsJunkie:~$ sudo service docker start
 * Starting Docker: docker                                    [ OK ]
abhijit@AwsJunkie:~$ sudo service docker status
 * Docker is running

Finally, verify by running hello-world docker image. docker run command will pull the docker image, run the same in a docker container and print the message "Hello from Docker!".

abhijit@AwsJunkie:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:aa0cc8055b82dc2509bed2e19b275c8f463506616377219d9642221ab53cf9fe
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
:

Troubleshooting

Issue: sudo service docker start says "Starting Docker: docker" but sudo service docker status shows "Docker is not running" and most docker commands throw message following message "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?".

abhijit@AwsJunkie:~$ sudo service docker start
 * Starting Docker: docker                                    [ OK ]
abhijit@AwsJunkie:~$ sudo service docker status
 * Docker is not running
abhijit@AwsJunkie:~$ docker version
Client: Docker Engine - Community
 Version:           23.0.0
 API version:       1.42
 Go version:        go1.19.5
 Git commit:        e92dd87
 Built:             Wed Feb  1 17:47:51 2023
 OS/Arch:           linux/amd64
 Context:           default
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Fix:

Fix: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Issue: sudo service docker start says “Starting Docker: docker” but sudo service docker status shows “Docker is not running” and most docker commands throw message following message “Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?”. abhijit@AwsJunki…

References