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'.

Developing code with WSL: 1 - Common problem

<WIP>

The two things Microsoft did recent years I would give a thumb up are WSL and VisualStudio Code. Both are gaining a lot of attraction along all software developers, I believe. Now, I'd like to make some note with WSL first. I had talked about WSL before, as:
Feb 2019: Running Linux on Windows

Jun 2020: Windows10 运行Ubuntu - WSL2

I'm going to create multiple post for what I learned while using WSL. This first one would be some common tips. 

Accessing files cross host Windows and WSL filesystem 

share folder: /mnt/c

running windows application

share git: cache issue

If user frequently or occasionally would switch between Windows and WSL, and run git command, may see that git will refresh index. As explained from this stackoverflow post:

  • The thing you're using here, which Git variously calls the index, the staging area, or the cache, does in fact contain cache data.

  • The cache data that it contains is the result of system calls.

  • The system call data returned by a Linux system is different from the system call data returned by a Windows system.

Especially if have VS-Code installed on Windows, and w/o using WSL extension to access the repo, VS-Code will run the windows native git.exe with the GitLens extension.

Another thing user may need to consider is: under WSL, the host Windows' git.exe is faster than the WSL linux git. So a simple and efficient solution would be set an alias by adding one line in ~/.bashrc file with:

alias git=git.exe

With this, under WSL it will always invoke the Windows git.exe for fetching the index. It's faster, no refresh when switch between Windows and WSL Linux. A side note is this won't work for tcl/expect which is used by the autobld script. This probably is due to the way how WSL is handling running navtive Windows app under WSL. For that, we can take the other approach mentioned in the same stackoverflow post: set environment variable of GIT_INDEX_FILE

 Makefile : Clock skew detected

Makefile : Clock skew detected - Stack Overflow, this is not the case for WSL build. In fact: WSL2 Clock is out of sync with Windows - Stack Overflow. Another easy way to get around this is: close all WSL session, then from a console, issue: wsl --shutdown. Then open a new WSL session, it will automatically sync the time with Windows host.

GUI: Windows 11, or MobaXterm and 32 bit/64bit executable

Refer to Run Linux on Windows - WSL