DEV Community

klo2k
klo2k

Posted on

Run Docker in WSL2 in 5 minutes (via systemd, without Docker Desktop!)

Now WSL2 has systemd support, we can run Docker in WSL without Docker desktop!

TL;DR

  1. Ensure /etc/wsl.conf has

    [boot]
    systemd=true
    
  2. Restart WSL

    wsl --shutdown
    wsl --distribution Ubuntu
    
  3. Install docker-cli - see my guide

  4. Log back into WSL > Profit 💫

Complete Guide

  1. Ensure WSL is up-to-date:

    wsl --update
    
  2. (Optional) Install + configure Ubuntu distro (if you haven't already):

    wsl --install Ubuntu
    
  3. Configure WSL (assuming "Ubuntu" is your distro):

    # Open up "Ubuntu"
    wsl --distribution Ubuntu
    
    # Enable systemd via /etc/wsl.conf
    {
    cat <<EOT
    [boot]
    systemd=true
    EOT
    } | sudo tee /etc/wsl.conf
    
    exit
    
  4. Restart WSL:

    wsl --shutdown
    wsl --distribution Ubuntu
    
  5. Check systemd is running - You should see 'OK: Systemd is running' message:

    systemctl --no-pager status user.slice > /dev/null 2>&1 && echo 'OK: Systemd is running' || echo 'FAIL: Systemd not running'
    
  6. Install docker-ce (cmds from my post):

    # Install Docker
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    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
    sudo apt update
    sudo apt install -y docker-ce docker-compose-plugin
    
    # Install docker-compose
    sudo ln -s /usr/libexec/docker/cli-plugins/docker-compose /usr/bin/docker-compose
    
    # Add yourself to the docker group
    sudo usermod -aG docker $USER
    
    # Exit bash - this is important!!!
    exit
    
  7. Re-login (this is important!):

    wsl --distribution Ubuntu
    
  8. You can now run docker-cli without Docker Desktop 🙂:

    docker run --rm -it hello-world
    

Top comments (2)

Collapse
 
obriat profile image
O'Briat

Systemd works but docker doesn't start :

journalctl -xeu docker.service
â–‘â–‘ The job identifier is 460 and the job result is failed.
Jan 06 10:33:13 KCI-QXqvRrQSpqN systemd[1]: docker.service: Start request repeated too quickly.
Jan 06 10:33:13 KCI-QXqvRrQSpqN systemd[1]: docker.service: Failed with result 'exit-code'.
â–‘â–‘ Subject: Unit failed
â–‘â–‘ Defined-By: systemd
â–‘â–‘ Support: http://www.ubuntu.com/support
â–‘â–‘
â–‘â–‘ The unit docker.service has entered the 'failed' state with result 'exit-code'.
Jan 06 10:33:13 KCI-QXqvRrQSpqN systemd[1]: Failed to start Docker Application Container Engine.
Enter fullscreen mode Exit fullscreen mode

I use this workaround for docker :

sudo vi /etc/wsl.conf
...
[boot]
#systemd=true
command = sudo service docker start
Enter fullscreen mode Exit fullscreen mode
Collapse
 
edemir206 profile image
Edemir Santos de Paula • Edited

Does it work with Debian ? Tryied but says systemd is not running.

FAIL: Systemd not running

Edit: I'm using windows 10, and according to this link Systemd needs windows 11.