Skip to content

Instantly share code, notes, and snippets.

@AuruTus
Last active July 5, 2024 14:52
Show Gist options
  • Select an option

  • Save AuruTus/0ff57fa885e666c44cc7cb51b3602013 to your computer and use it in GitHub Desktop.

Select an option

Save AuruTus/0ff57fa885e666c44cc7cb51b3602013 to your computer and use it in GitHub Desktop.
How to Install Docker in WSL2 and auto-boot with systemd

Install Guide

Table of Contents

NOTE

This is the guide for WSL2 with Ubuntu distro. Before you start, check your WSL distro version with wsl --list -v, and it will show the VERSION with 2 if correct.

And to start systemd of WSL2, just edit the /etc/wsl.conf, and add the configuration bellow. (You can use sudo vim /etc/wsl.conf)

[boot]
systemd=true

Ref: wsl official dev-blog

After the configuring, terminate the WSL2 server with wsl --shutdown ( or wsl --terminate <your distro name>) and restart it. Then you can check if the systemd is running with

systemctl --no-pager status user.slice > /dev/null 2>&1 && echo 'OK: Systemd is running' || echo 'FAIL: Systemd not running'

PRE-REQUIETIES

Install pre-requieties for docker:

sudo apt update && sudo apt upgrade
sudo apt install --no-install-recommends apt-transport-https ca-certificates curl gnupg2

IMPORTANT: And then configure the iptables:

sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy

INSTALL DOCKER

Now we can install docker.

First we need to update the apt source for docker.

. /etc/os-release # load env vars form os-release
curl -fsSL https://download.docker.com/linux/${ID}/gpg | sudo tee /etc/apt/trusted.gpg.d/docker.asc
echo "deb [arch=amd64] https://download.docker.com/linux/${ID} ${VERSION_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt update

And then just install it with apt:

sudo apt install docker-ce docker-ce-cli containerd.io

CONFIGURE USER POLICY

We need add the current user in docker group manually.

sudo usermod -aG docker $USER

To confirm the change worked, close the terminal tab and open a new Ubuntu tab, then run:

groups | grep docker

You should see docker in the content. And then terminate the WSL2 again (wsl --shutdown).

ENABLE DOCKER TO AUTO-BOOT

Finally, use:

sudo systemctl enable docker.service

or:

sudo service docker start

It can be checked via:

systemctl list-units --type=service

And you should see docker.service is not red, and is with active and running states.

CONFIG HTTP PROXY (IF NEEDED)

If you need add proxy for docker daemon, just add the Systemd configure:

sudo mkdir -p /etc/systemd/system/docker.service.d
sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf

and add these lines:

[Service] 
Environment="HTTP_PROXY=http://<Your Proxy IP or Domain>:<port>" 
Environment="HTTPS_PROXY=http://<Your Proxy IP or Domain>:<port>"

Then restart your Docker service with new config:

sudo systemctl daemon-reload 
sudo systemctl restart docker 

And check the new env var with:

sudo systemctl show --property=Environment docker

You should see something like:

Environment=HTTP_PROXY=http://<Your Proxy IP or Domain>:<port> HTTPS_PROXY=http://<Your Proxy IP or Domain>:<port>

TRY DOCKER

You can try docker to see if it can connect to the dockerd:

docker run --rm hello-world

Refs:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment