Saturday, September 2, 2023

Developing code with WSL: 2 - Install docker

(continue of Developing code with WSL: 1 - Common problem)

The easy way to try docker with WSL would be installing Docker Desktop on host Windows. Some detail can be found from Microsoft tutorial site: Get started with Docker containers on WSL | Microsoft Learn. But if you prefer to decouple Docker with the host OS, just want to install everything under WSL, you can following Docker Linux installation instruction to just install Docker engine, such as instruction here: https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository

When run  ~$ docker run hello-world, might get this:

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

If google this, may see this got asked on Stack Overflow: ubuntu - docker:Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

The suggestion there is run: systemctl start docker && systemctl enable docker. Then may get another error message:

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
 

This is due to limitation of older version of WSL which does not support systemd, or systemd flag has not been set in WSL setting. In that case, try: sudo service docker start. Systemd is supported with WSL version 0.67.6 or newer(reference from Microsoft: Systemd support is now available in WSL! - Windows Command Line). As the link mentioned, if checking WSL version with 'wsl --version' failed, it means  you are running the in-Windows version of WSL and need to upgrade to the Store version.

To enable systemd:You will need to edit the wsl.conf file to ensure systemd starts up on boot.Add these lines to the /etc/wsl.conf with sudo:

[boot]
systemd=true

With that, run  ~$ docker run hello-world would get this:

~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

If you prefer not to use 'sudo' for docker operation, may refer to this 'Ask Ubuntu' post. It works for docker 0.5.3 and up. Basically you would need to add the desired user to 'docker' group, like this:

sudo gpasswd -a $USER docker

Then re-login this user if it is the current user. For WSL, you can run 'logout' and you will automatically get re-login. After login, should be able to run docker command without 'sudo'.

0 Comments:

Post a Comment