注意:安装WSL2对操作系统有要求:至少Windows 10 1903 (Build 18362) 。

官方安装方式

如果只是想在WSL上安装docker,在windows上用起来,建议参考微软官方文档安装Docker Desktop。

地址 https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-containers

在WSL的Terminal中安装Docker

比较折腾的安装方式,推荐不想装个docker桌面应用的玩家。

安装WSL

如果没有安装过可以按照官方教程安装。

地址 https://docs.microsoft.com/en-us/windows/wsl/install

从WSL1切换到WSL2

我装的 distro = Ubuntu-20.04 。

因为事前已经装过WSL,但按照docker的安装要求,需要WSL版本是2 。

PS C:\Users\overlord> wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-20.04    Stopped         1

用命令 wsl -l -v 查看发现是1 。

在调用 wsl --set-version Ubuntu-20.04 2 之前,须安装 linux-kernel update package

须启用Windows功能:
Hyper-V设置

我在启用Hyper-V虚拟机监控程序之后发生了HDMI的Audio驱动丢失的现象,关闭后再启用就好了。

win+r快捷键输入msinfo32.exe查看是否启用成功:
虚拟化启用成功

再在powershell里输入命令 wsl --set-version Ubuntu-20.04 2 。等待转换完成。

PS C:\Users\overlord> wsl --set-version Ubuntu-20.04 2
正在进行转换,这可能需要几分钟时间...
有关与 WSL 2 的主要区别的信息,请访问 https://aka.ms/wsl2
转换完成。
PS C:\Users\overlord> wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-20.04    Stopped         2

用 wsl -l -v 命令可检测wsl版本是否已转换。

安装设置运行Docker

打开WSL的Terminal命令行界面安装docker。

 ~  sudo apt update
 ~  sudo apt install docker.io -y

用docker --version命令查看是否安装成功。

 ~  docker --version
Docker version 20.10.7, build 20.10.7-0ubuntu5~20.04.2

但此时并不能实际运行docker容器,因为docker运行依赖的daemon服务还没有在后台运行。

先给daemon服务的启动程序dockerd添加sudo权限。

 ~  sudo visudo

sudo visudo 后会进入nano文本编辑器的界面,编辑的文件是 /etc/sudoers ,在尾部另起行添加内容:

# Docker daemon specification
$USER ALL=(ALL) NOPASSWD: /usr/bin/dockerd

$USER替换为你登录WSL的用户名。

给每次登入WSL子系统添加自动启动dockerd程序的启动项。

根据你使用的WSL终端,这个启动文件会添加在~/.bashrc或者~/.zshrc里,登录哪个终端在哪个终端的启动项文件里加。

# Start Docker daemon automatically when logging in if not running.
RUNNING=`ps aux | grep dockerd | grep -v grep`
if [ -z "$RUNNING" ]; then
    sudo dockerd > /dev/null 2>&1 &
    disown
fi

把当前用户组添加到docker组。

 ~  sudo usermod -aG docker $USER

配置完成后重启WSL的Terminal,运行docker run hello-world测试。

 ~  docker run hello-world

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/

显示此信息即配置成功。

安装运行Docker-Compose

直接装V2,不用短横线直接空格使用docker compose 。

 ~  mkdir -p ~/.docker/cli-plugins/
 ~  curl -SL https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose

github下载不下来时试试走代理。

下载完毕后赋予docker-compose V2 运行权限。

 ~  chmod +x ~/.docker/cli-plugins/docker-compose

检测一下能否正常运行

 ~  docker compose version
Docker Compose version v2.2.3

建立编辑docker-compose.yml配置文件。

 ~  nano docker-compose.yml

在编辑器中填入如下内容,退出保存。

version: "3.9"
services:
  hello:
    image: "hello-world"

尝试运行docker compose 。

 ~  docker compose run hello

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/

若如上显示则docker compose安装成功。

参考资料

https://medium.com/geekculture/run-docker-in-windows-10-11-wsl-without-docker-desktop-a2a7eb90556d
https://docs.docker.com/compose/cli-command/
https://docs.microsoft.com/en-us/windows/wsl/install-manual#step-4---download-the-linux-kernel-update-package
https://dev.to/bowmanjd/install-docker-on-windows-wsl-without-docker-desktop-34m9


overlordsnyt
14 声望1 粉丝