The tutorial of installing docker in Ubuntu (WSL) of win10 subsystem

Time:2021-6-2

Environmental description

Environment: win10 enterprise + Ubuntu 18.04 lts [WSL]

Installing docker for windows in win10

Now docker has a special installation package for win10 professional system, and Hyper-V needs to be started.

Turn on Hyper-V

在这里插入图片描述

Procedures and functions

在这里插入图片描述

Turn windows features on or off

在这里插入图片描述

Select Hyper-V

在这里插入图片描述

Install docker for windows

The latest version of docker for windows

Download address: https://docs.docker.com/v17.09/docker-for-windows/install/

Click Get docker for windows [stable] and download the version of windows:

在这里插入图片描述

After installation, docker will start automatically. A little whale icon will appear on the notice bar在这里插入图片描述, which means docker is running.

Three icons will also appear at the table, as shown in the figure below:

We can execute it on the command linedocker version To see the version number,docker run hello-world To load the test image test.

If it doesn’t start, you can search docker in windows to start it:

在这里插入图片描述

After startup, you can also see the little whale icon on the notification bar:

在这里插入图片描述

Subsystem installation docker installation

No problem


[email protected]:~$ sudo apt-get install docker.io
Reading package lists... Done
Building dependency tree
Reading state information... Done
docker.io is already the newest version (18.09.7-0ubuntu1~18.04.4).
0 upgraded, 0 newly installed, 0 to remove and 111 not upgraded.

There is something wrong with the use

Explanation of official website

[email protected]:~$ docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[email protected]:~$ sudo /etc/init.d/docker status
 * Docker is not running
[email protected]:~$ sudo /etc/init.d/docker start
 * Starting Docker: docker                                             [ OK ] 
[email protected]:~$ 
[email protected]:~$ sudo /etc/init.d/docker status
 * Docker is running
[email protected]:~$ sudo /etc/init.d/docker status
 * Docker is not running

You will find that the docker can't be started. What should we do now? Be sure to read the log first (/ var / log / docker. Log)
[email protected]:~$ tail /var/log/docker.log -n 100

time="2019-10-22T11:58:25.436968100+08:00" level=info msg="ccResolverWrapper: sending new addresses to cc: [{unix:///var/run/docker/containerd/containerd.sock 0 <nil>}]" module=grpc
time="2019-10-22T11:58:25.437359900+08:00" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
time="2019-10-22T11:58:25.437703900+08:00" level=info msg="pickfirstBalancer: HandleSubConnStateChange: 0xc420769030, CONNECTING" module=grpc
time="2019-10-22T11:58:25.438698400+08:00" level=info msg="pickfirstBalancer: HandleSubConnStateChange: 0xc420769030, READY" module=grpc
time="2019-10-22T11:58:25.559058900+08:00" level=info msg="[graphdriver] using prior storage driver: overlay2"
time="2019-10-22T11:58:25.719182400+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds"
time="2019-10-22T11:58:25.719593400+08:00" level=warning msg="Your kernel does not support cgroup memory limit"
time="2019-10-22T11:58:25.719660300+08:00" level=warning msg="Unable to find cpu cgroup in mounts"
time="2019-10-22T11:58:25.719697200+08:00" level=warning msg="Unable to find blkio cgroup in mounts"
time="2019-10-22T11:58:25.719732900+08:00" level=warning msg="Unable to find cpuset cgroup in mounts"
time="2019-10-22T11:58:25.719888000+08:00" level=warning msg="mountpoint for pids not found"
time="2019-10-22T11:58:25.721991900+08:00" level=info msg="stopping healthcheck following graceful shutdown" module=libcontainerd
time="2019-10-22T11:58:25.722045300+08:00" level=info msg="stopping event stream following graceful shutdown" error="context canceled" module=libcontainerd namespace=plugins.moby
time="2019-10-22T11:58:25.723869300+08:00" level=info msg="pickfirstBalancer: HandleSubConnStateChange: 0xc420769030, TRANSIENT_FAILURE" module=grpc
time="2019-10-22T11:58:25.724042800+08:00" level=info msg="pickfirstBalancer: HandleSubConnStateChange: 0xc420769030, CONNECTING" module=grpc
Error starting daemon: Devices cgroup isn't mounted

Preliminary conclusion: win10 kernel can't fully simulate CGroup and aufs components of Linux

Official website explanation [solution]

[Cross Post] WSL Interoperability with Docker

https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly

apt install screenfetch
screenfetch

script: https://files.cnblogs.com/files/rgqancy/WSL.js , download and save as TXT

Establish relay

Now with go installed, we can build relays. In the following command, make sure to replace it with your Windows user name:


go get -d github.com/jstarks/npiperelay
GOOS=windows go build -o /mnt/c/Users/Administrator/go/bin/npiperelay.exe github.com/jstarks/npiperelay

We have now built a relay for windows, but we want it to be called from within WSL. To do this, we create a symbolic link. Make sure to replace with your Windows user name:


sudo ln -s /mnt/c/Users/Administrator/go/bin/npiperelay.exe /usr/local/bin/npiperelay.exe

We will use socat to help enable relaying. Install socat, which is a tool that allows two-way flow of data between two points. Grab this package:


sudo apt install socat

The last step

With the installation of socat and the construction of executable files, we only need to string a few things together. We will make a shell script to activate our function. We will put it in the user’s home directory. To do this:


#make the file
touch ~/docker-relay
#add execution privileges
chmod +x ~/docker-relay

Open the file we created with your favorite text editor (such as VIM). Paste it into the file docker relay:


#!/bin/sh
exec socat UNIX-LISTEN:/var/run/docker.sock,fork,group=docker,umask=007 EXEC:"npiperelay.exe -ep -s //./pipe/docker_engine",nofork

Save the file and close it. The docker relay script configures the docker pipeline to allow docker group access. To run as a normal user (without attaching ‘sudo’ to each docker command), add your WSL user to the docker group. In Ubuntu:


sudo adduser <your WSL user> docker

Test it!

Open a new WSL shell to ensure that your group membership is reset. Start the relay in the background:


sudo ~/docker-relay &

Now run the docker command to test the waters. You should be welcomed by the same output as if you were running a command from windows (and note that you don’t need to add ‘sudo’ before the command!)

be careful

To start docker for windows in advance

在这里插入图片描述

schematic diagram

There is a basic problem in making docker client run under WSL to communicate with docker for windows: WSL client understands IPC through UNIX socket, while docker for windows understands IPC through named pipe. That’s where socat and npiperalay.exe work – it’s the intermediary between the two disjoint forms of IPC. Socat understands how to communicate through UNIX sockets, npipereray understands how to communicate through named pipes. Both socat and npipereray know how to communicate through stdio, so they can communicate with each other.

在这里插入图片描述

summary

The above is a tutorial of installing docker in Ubuntu (WSL), the win10 subsystem introduced by Xiaobian. I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply you in time. Thank you very much for your support to developer!
If you think this article is helpful to you, please reprint, please indicate the source, thank you!