使用 wsl2 而非 Docker desktop 安装 Docker,体验完全接近 Linux 的使用 Docekr 方式。并解决 wsl2 中 Docker 启动不了的问题。
一、安装 wsl2
官方文档:https://learn.microsoft.com/zh-cn/windows/wsl/install-manual
- 启用 Linux 的 Windows 子系统
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
- 启用虚拟机功能
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
- 下载 Linux 内核更新包
- amd64(大部分电脑)
https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
- arm64
https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_arm64.msi
- 将 WSL 2 设置为默认版本
wsl --set-default-version 2
- 安装所选的 Linux 分发
打开微软商店,搜例如 Ubuntu,选择合适的版本下载安装即可。
二、安装 Docker
官方文档:https://docs.docker.com/engine/install/ubuntu/
- 卸载旧版本
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
- 配置 docker 仓库
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Add the repository to Apt sources:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
注:
- 若出现域名无法解析的错误,可尝试使用阿里云的 DNS 服务器。
vim /etc/resolv.conf
nameserver 114.114.114.114
- 若出现源不可用/速度慢的情况,可尝试使用国内的 apt 镜像源。
vim /etc/apt/sources.list
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
- 安装 Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
- 加入 Docker 用户组
sudo usermod -aG docker $USER
exit # 执行后重新再打开终端
- 配置防火墙
为什么需要多这一步?因为 wsl2 系统中使用的是经过修改的 nftables,而 Docker 安装程序使用 iptables 进行 NAT。为了解决这个问题,必须使用以下命令将系统切换回使用传统的 iptables,否则无法启动成功。
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
- 配置 Docker
/etc/docker/daemon.json
{
"insecure-registries": ["registry.cn-hangzhou.aliyuncs.com"],
"registry-mirrors": [
"https://uh06sk44.mirror.aliyuncs.com",
"http://registry.cn-hangzhou.aliyuncs.com"
],
"log-driver": "json-file",
"log-opts": { "max-size": "500m", "max-file": "3" }
}
- 启动 Docker,并配置自启
sudo systemctl enable docker
# 也可以用此命令启动了
sudo systemctl start docker
- 配置 WSL 开机自启
用户目录下新建 .wslconfig 文件,添加systemd=true
, 在此也可以配置 cpu 和内存等。
# Settings apply across all Linux distros running on WSL 2
[wsl2]
# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=4GB
# Sets the VM to use two virtual processors
processors=4
# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=0
# Turn off default connection to bind WSL 2 localhost to Windows localhost
localhostForwarding=true
# Support systemcel cmd
systemd=true
- 检测状态
进入 Linux 子系统后执行命令,输出systemd
则开启,init
则是关闭
$ ps --no-headers -o comm 1
systemd
标签:sudo,wsl,apt,https,ubuntu,docker,安装,Docker
From: https://www.cnblogs.com/longkang/p/18245959