win10子系统ubuntu(WSL) 安装Docker的教程(图文详解)

  • 来源:网络
  • 更新日期:2020-09-23

摘要:服务器 环境说明 环境:win10企业版 + ubuntu 18.04 LTS [WSL] win10安装Docker for windows 现在 Docker 有专门的 Win10 专业版系统的安

服务器 环境说明

环境:win10企业版 + ubuntu 18.04 LTS [WSL]

win10安装Docker for windows

现在 Docker 有专门的 Win10 专业版系统的安装包,需要开启Hyper-V。

开启 Hyper-V


程序和功能



启用或关闭Windows功能



选中Hyper-V


安装Docker for windows

最新版 Docker for windows

下载地址: https://docs.docker.com/v17.09/docker-for-windows/install/

点击 Get Docker for Windows [Stable],并下载 Windows 的版本:


安装完成后,Docker 会自动启动。通知栏上会出现个小鲸鱼的图标,这表示 Docker 正在运行。

桌边也会出现三个图标,入下图所示:

我们可以在命令行执行 docker version 来查看版本号,docker run hello-world 来载入测试镜像测试。

如果没启动,你可以在 Windows 搜索 Docker 来启动:



启动后,也可以在通知栏上看到小鲸鱼图标:


子系统安装Docker安装

没啥问题

leenhem@DESKTOP-7SREO20:~$ 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.

使用 有问题 [无法正常使用]

官网释疑【解

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

你会发现这个Docker怎么都启动不了,那现在怎么办?一定要先看日志(/var/log/docker.log )
leenhem@DESKTOP-7SREO20:~$ 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

初步结论:应该WIN10内核无法完全模拟Linux的cgroup和aufs组件

官网解释【解决方案】

[Cross Post] WSL Interoperability with Docker

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

apt install screenfetch
screenfetch

脚本:https://files.cnblogs.com/files/rgqancy/WSL.js,下载后另存为txt

建立中继

现在安装Go,我们可以构建继电器。在下面的命令中,请确保使用您的Windows用户名替换:

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

我们现在已经为Windows构建了中继,但我们希望它可以从WSL内部调用。为此,我们创建一个符号链接。确保使用您的Windows用户名替换:

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

我们将使用socat来帮助启用中继。安装socat,这是一种允许两点间数据双向流动的工具(稍后详细介绍)。抓住这个包裹:

sudo apt install socat

最后一步

随着socat的安装和可执行文件的构建,我们只需要将几件事情串起来。我们将制作一个shell脚本来激活我们的功能。我们将把它放在用户的主目录中。去做这个:

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

打开我们用你最喜欢的文本编辑器创建的文件(比如vim)。将其粘贴到文件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

保存该文件并关闭它。docker-relay脚本将Docker管道配置为允许docker组访问。要以普通用户身份运行(不必为每个docker命令附加\'sudo\'),请将您的WSL用户添加到docker组。在Ubuntu中:

sudo adduser <your WSL user> docker

测试一下吧!

打开一个新的WSL外壳以确保您的组成员身份被重置。在后台启动中继:

sudo ~/docker-relay &

现在运行docker命令来测试水域。你应该被相同的输出迎接,就像你从Windows运行命令一样(并且注意你不需要在命令前加\'sudo\'!)

注意

要提前启动docker for windows


原理图

使Docker客户端在WSL下运行以与Docker for Windows通信存在一个基本问题:WSL客户端通过unix套接字理解IPC,而Docker for Windows通过命名管道理解IPC。这就是socat和npiperelay.exe发挥作用的地方–这是这两种不相交IPC形式之间的中介。Socat理解如何通过unix套接字进行通信,npiperelay理解如何通过命名管道进行通信。Socat和npiperelay都了解如何通过stdio进行通信,因此它们可以彼此通信。


总结

以上所述是小编给大家介绍的win10子系统ubuntu(WSL) 安装Docker的教程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

新网虚拟主机