Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to Install and Use Docker in WSL2
Ryan D. Lewis
Ryan D. Lewis

Posted on • Originally published atryandlewis.dev

     

How to Install and Use Docker in WSL2

Edit: It's come to my attention that, since I figured out this workaround back when WSL2 and thus Docker's WSL2 backend were new, Docker Desktop for Windows has added support for using Docker from within your WSL2 distro. This obviates the need to install Docker within a WSL2 distro in most cases. But if you find yourself in a position where you can't or don't want to use the Docker Desktop support, read on.

Say you want to run a Linux environment on a Windows machine, and in that environment one of the things you want to do is make use of docker containers. Here's the quick and dirty way to get that set up:

Install WSL

Nowadays, this should be as simple asWin+X, selecting<Command Prompt/Powershell/Windows Terminal> (Admin), and runningwsl --install.

If that doesn't work, or you want to fiddle/customize/use a non-default distro, check outMicrosoft's guide here. Make sure you install a WSL2 distro.

For the rest of this, I'm assuming you've installed the default Ubuntu Distro, steps might be slightly different for other distros.

Verify and Setup WSL

Make sure that the distro you just installed is a WSL2 distro, as you can't run docker in WSL1.

# Set the default version to 2wsl --set-default-version 2# Check that the distro you installed is version 2wsl -l -v# Upgrade a v1 distro to v2wsl --set-version <distro-name> 2
Enter fullscreen modeExit fullscreen mode

If you're having trouble upgrading the distro, seehere for help.

Now, open the "Ubuntu" application that you just got installed, and set your username and password. Do package updates, install whatever tools and packages you want, and just generally make yourself at home.

Install Docker

Followthese setup instructions (if you chose to install a distro other than Ubuntu, find the appropriate install guide on the left of that page).

Stop before runningsudo docker run hello-world.

Configuring Docker on WSL2

Using Docker Without Invoking Root

Don't want to have to run docker commands withsudo all the time?Follow this guide to add yourself to the docker group.

Starting the Docker Daemon

One hiccup with docker in WSL2 is that it doesn't automatically start the Docker service. The simple but annoying solution is to runsudo service docker start whenever you want to use Docker.

If you don't want to have to remember and invoke that command every time, you can add the following to your "~/.profile", or your shell configuration file like "~/.bashrc":

if [ -n "`service docker status | grep not`" ]; then    sudo /usr/sbin/service docker startfi
Enter fullscreen modeExit fullscreen mode

Now this has the annoying side effect of making you have to type out your sudo password whenever you start WSL2 for the first time.

To fix this, you can runsudo visudo -f /etc/sudoers.d/passwordless_docker_start, add the following to the file (replacingusername with your Linux username), save and close. (See here to learn more about the intricacies and nuances of sudoer files)

username        ALL = (root) NOPASSWD: /usr/sbin/service docker start
Enter fullscreen modeExit fullscreen mode

Now, the docker service automatically starts in WSL2 without requiring authentication, and you can use it more or less exactly like you would use Docker on a regular Linux install.

Cover Photo byTom Fisk fromPexels.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
raan profile image
Piotr Jarosz
  • Joined

Instead of adding user to sudoers file, you could just add your user to docker group. It will do the job.
sudo usermod -aG docker username

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a student/developer/researcher/roboticist trying to write free and open source code in his free time.
  • Location
    United States
  • Education
    B.S. in CS and Computational Mathematics, M.S. in CS, Grad Student in Robotics
  • Work
    Web Applications Developer @ Mcity
  • Joined

More fromRyan D. Lewis

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp