在WSL2(用於Linux的Windows子系統v2)用命令列安裝docker及docker compose

overlordsnyt發表於2022-11-24

注意:安裝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

相關文章