留神:装置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虚拟机监控程序之后产生了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 --versionDocker 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 &    disownfi

把以后用户组增加到docker组。

 ~  sudo usermod -aG docker $USER

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

 ~  docker run hello-worldHello 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 bashShare 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 versionDocker 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 helloHello 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 bashShare 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