关于docker:在WSL2用于Linux的Windows子系统v2用命令行安装docker及docker-compose

3次阅读

共计 3838 个字符,预计需要花费 10 分钟才能阅读完成。

留神:装置 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 --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

正文完
 0