WSL 运行 docker 容器

Linux子系统(Windows Subsystem for Linux, WSL)是Windows10上的一个模拟环境,因而并不能完全实现LInux中的各种操作,而且性能也不如一般的虚拟机。得益于此,WSL与Windows交互非常方便,消耗非常低,没有启动时间,用来做轻量开发或测试很适合。

网上关于如何在WSL中安装Docker有很多教程,大多时间较早,都面临着需要安装Docker for Windows的问题,这就需要运行虚拟机,对于我资源吃紧的笔记本还是有些负担,而且对于没有Win10 Pro(Hyper-V)的用户也不方便安装。Win10创意者更新后,在WSL中运行Docker Engine成为可能,当然这个功能正在逐步完善中,当前并不能支持docker全部指令

本机环境

Windows10 Pro 1903

img

安装WSL

设置==>更新安全==>开发者选项==>旁加载应用(选择此模式即可) Microsoft Store==>搜索Linux==>安装Ubuntu18.04(可选Ubuntu16.04)

img

安装Docker

首先执行以下命令安装Docker

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 下载docker 17.09.0版本【必须使用此版本】
curl -O https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/docker-ce_17.09.0~ce-0~ubuntu_amd64.deb
# 安装docker
sudo dpkg -i docker-ce_17.09.0~ce-0~ubuntu_amd64.deb
# 安装依赖
sudo apt -y -f install
# 修改docker权限(非必须)
sudo usermod -aG docker $USER
# 安装cgroupfs-mount
sudo apt -y install cgroupfs-mount

以管理员身份启动WSL控制台,执行以下命令启动docker

1
2
sudo cgroupfs-mount
sudo service docker start

测试Docker

测试安装结果

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$ docker version
Client:
 Version:      17.09.0-ce
 API version:  1.32
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:42:18 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.09.0-ce
 API version:  1.32 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:40:56 2017
 OS/Arch:      linux/amd64
 Experimental: false

测试运行容器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete                                                                                             Digest: sha256:92695bc579f31df7a63da6922075d0666e565ceccad16b59c3374d2cf4e8e50e
Status: Downloaded newer image for hello-world:latest

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

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

显示如上信息,就表示可以正常使用了。

需要注意的是每次电脑重启后先执行cgroupfs-mount再启动docker服务。

配置

docker开启远程访问

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 默认情况下,Docker守护进程Unix socket(/var/run/docker.sock)来进行本地进程通信,而不会监听任何端口,
# 因此只能在本地使用docker客户端或者使用Docker API进行操作。如果想在其他主机上操作Docker主机,
# 就需要让Docker守护进程打开一个HTTP Socket,这样才能实现远程通信。

# 编辑docker的配置文件/etc/default/docker修改DOCKER_OPTS
# 同时监听本地unix socket和远程http socket(2375)
DOCKER_OPTS="-H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375"

# 然后重新启动docker守护进程。
sudo service docker restart

# 此时发现docker守护进程已经在监听2375端口,在另一台主机上可以通过该端口访问Docker进程了。
docker -H IP:2375 images

# 本地操作docker。
docker images
Licensed under CC BY-NC-SA 4.0