Using Docker in WSL

     

The saga continues, we’re still trying to get to the perfect Ansible development environment on Windows using Windows Subsystem for Linux (WSL). Now WSL should have working systemd, it’s time to set up Docker.

First of all, install Docker Desktop for Windows, obviously. Once done, go to Settings, and make sure you’re using the WSL 2 based engine:

Then also ensure it’s offered inside your WSL distros. Go to Resources / WSL integration. Here you may enable it for the default distro, or individually for each distro installed:

Then open a terminal for your WSL distro and check if the docker command is available:

$ docker --version
Docker version 20.10.24, build 297e128

If you get -bash: /usr/bin/docker: No such file or directory, ensure that Docker is indeed running on Windows, because it “injects” these executables into your WSL distros:

$ ls -al /bin/docker
lrwxrwxrwx 1 root root 48 Apr 10 20:50 /bin/docker -> /mnt/wsl/docker-desktop/cli-tools/usr/bin/docker

Therefore, if Docker Desktop isn’t running, you can’t even call these executables inside WSL.

Now try to run a container. I can’t really remember if this was done automatically or not, so you may or may not get the following error first:

$ docker run -it --rm ubuntu
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create": dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.

In this case add yourself to the docker Unix group first:

sudo usermod --append --groups docker bviktor

Then exit your existing WSL shells, reopen them, and try again. Now it should be good:

$ docker run -it --rm ubuntu
root@1e227a618e3f:/#

Oh yesss. There you have it, Docker integrated with WSL. Stay tuned, we have a lot more to get to that Ansible Galaxy development environment.