DEV Community

klo2k
klo2k

Posted on

14 1

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
    

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

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

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.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay