Win10-WSL-Ubuntu20.04-Docker 安装与配置

一、WSL 安装 Ubuntu20.04

  1. Win10 启用 WSL

管理员身份运行:

1
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

重启电脑。

开启虚拟器特性

1
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

下载安装包:

设置默认版本:

1
wsl --set-default-version 2

ref: https://docs.microsoft.com/en-us/windows/wsl/install-win10

  1. 打开 Microsoft Store,安装 Ubuntu20.04

  2. 启动 Ubuntu20.04,创建账号、密码

  3. 安装 Windows Terminal(可选)

用 scoop 安装。

1
scoop install windows-terminal

windows-terminal 简单设置:

  • 启动目录: //wsl$/Ubuntu-20.04//home/bo
  • 字体: Jetbrains Mono
  1. 设置源,更新(可选)

打开配置文件: sudo nano /etc/apt/sources.list, 替换成以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

ref: https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

更新:sudo apt update

升级:sudo apt upgrade

  1. 安装 ZSH、oh-my-zsh(可选)

参考 zsh & oh-my-zsh 安装配置

二、安装 Docker

ref: https://docs.docker.com/engine/install/ubuntu/

  1. 首先更新索引,安装必要的依赖软件,添加新的 HTTPS 软件源

    1
    2
    3
    4
    5
    6
    7
    sudo apt update

    sudo apt install apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
  2. 使用下面的 curl 导入源仓库的 GPG key

    1
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  3. 添加 Docker APT 软件源

    1
    2
    3
    echo \
    "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

现在,Docker 软件源被启用了,你可以安装软件源中任何可用的 Docker 版本。

  1. 安装 Docker 最新版本

    1
    2
    3
    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io

  2. 运行一下是否安装成功

    1
    sudo docker run hello-world

    可能或报错:

    1
    Cannot connect to the Docker daemon at unix:///var/run/docker.sock. > Is the docker daemon running?

再试一下:

1
2
sudo systemctl status docker
sudo systemctl status docker

还是报错:

1
2
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

原因是 WSL 没有完全实现 Linux 下的 systemctl 的命令, 可以检查一下:

1
2
3
4
5
6
7
8
# check if your system is using `systemd` or `sysvinit`
ps -p 1 -o comm=

# If the command doesn't return systemd, and in my case, Ubuntu-20.04 on WSL, the command returned init, then use the command pattern

# start services using sysvinit
service docker start

ref: https://stackoverflow.com/questions/52604068/using-wsl-ubuntu-app-system-has-not-been-booted-with-system-as-init-system-pi

因此,在WSL下 要用以下命令启动 docker。

1
service docker start

done!