Windows10在wsl2上面安装docker全攻略
作者:楽焫旒璡
1.安装wsl2
WSL2 是 “运行环境”:全称 Windows Subsystem for Linux 2,是 Windows 系统自带的功能。它通过虚拟化技术,让 Windows 能原生运行 Linux 内核,无需单独装虚拟机(如 VMware),启动快、资源占用低。
查看是否安装 wsl -v
PS C:\Users\guyin> wsl -v WSL 版本: 2.6.1.0 内核版本: 6.6.87.2-1 WSLg 版本: 1.0.66 MSRDC 版本: 1.2.6353 Direct3D 版本: 1.611.1-81528511 DXCore 版本: 10.0.26100.1-240331-1435.ge-release Windows: 10.0.26100.6899
若未安装, 右键开始-搜索-wsl, 若未安装wsl按下任意键自动下载安装, 或者powershell执行"wsl --install"自动下载安装并启用wsl2
2.升级Windows系统
我的系统是win11, 规格如下:
版本 Windows 11 家庭中文版
版本号 24H2
安装日期 2025/8/11
操作系统版本 26100.6899
如果操作系统版本为"10.0.26100.6899", 需要升级系统, 这个版本的系统无法安装Ubuntu, 升级后最后. "10.0.26100.7171"是可以安装ubuntu的(已验证).
3.安装ubuntu
Ubuntu 是 “适配的系统”:Ubuntu 是主流 Linux 发行版之一,微软已将其打包成应用,可在微软商店下载。安装后,它会作为 WSL2 的 “客户端”,借助 WSL2 的内核环境,实现和物理机 Ubuntu 几乎一致的命令行操作、软件安装(如 Docker、Python)。
3.1.确保BIOS/UEFI 中启用了虚拟化(VT-x / AMD-V)
如何检查?
- 按
Ctrl + Shift + Esc打开任务管理器 - 切到 “性能” → “CPU”
- 查看右下角:“虚拟化: 已启用”
如果显示 “已启用” → 跳到第二步
如果显示 “已禁用” → 需要进 BIOS 开启
如何开启虚拟化?
- 重启电脑,按 F2 / F10 / Del / Esc(不同品牌键不同)进入 BIOS
- 找到类似选项:
- Intel CPU: Intel Virtualization Technology (VT-x) → Enabled
- AMD CPU: SVM Mode 或 AMD-V → Enabled
- 保存并退出(通常按 F10)
💡 笔记本常见位置:
- Dell:
Processor Settings→Virtualization- Lenovo:
Security→Virtualization- HP:
System Configuration→Virtualization Technology
3.2.确保 Windows 功能已启用
以 管理员身份 打开 PowerShell 或 CMD,运行:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
然后重启电脑。
⚠️ 注意:必须启用 VirtualMachinePlatform,仅启用 WSL 是不够的!
你也可以通过图形界面检查:
- 按
Win + R→ 输入optionalfeatures - 确保勾选:
- ☑️ 适用于 Linux 的 Windows 子系统
- ☑️ 虚拟机平台
- 点确定并重启
3.3.设置 WSL2 为默认版本
重启后,在 PowerShell 中运行:
wsl --set-default-version 2
3.4.安装 Ubuntu
现在通过 Microsoft Store 安装 Ubuntu):
- 打开 Microsoft Store
- 搜索 “Ubuntu”
- 安装 Ubuntu(由 Canonical 提供)
- 安装完成后,从开始菜单启动它
打开之后要求创建普通用户和设置密码, root操作使用sudo执行
3.5.验证是否成功
安装成功后,运行:
wsl -l -v
输出应类似:
NAME STATE VERSION * Ubuntu Running 2
进入 WSL(默认进入powershell所在目录挂载的位置):
wsl
并测试:
uname -a # 应包含 "microsoft" 和 "WSL2"
3.6.补充说明
- 即使你在 Windows 11 Insider(Build 26100),也必须开启虚拟化,否则 WSL2 无法工作。
- 某些杀毒软件(如 360、McAfee)会阻止虚拟化,可临时关闭测试。
- Hyper-V 不是必须的,但 VirtualMachinePlatform 是 WSL2 的核心依赖。
4.迁移ubuntu工作目录
4.1.目录挂载说明
ubuntu系统文件目录如/home, /usr 是在C盘
- Windows 的各个盘符自动挂载在 WSL2 的
/mnt/目录下:
/mnt/c/ → 对应 Windows 的 C:\ /mnt/d/ → 对应 D:\ ...
- 例如:Windows 上的
C:\Users\guyin\Documents
在 Ubuntu 中路径是:
/mnt/c/Users/guyin/Documents
⚠️ 注意:不要在 /mnt/c/... 下运行 Python 项目或容器!
因为 NTFS ↔ Linux 文件系统跨层访问会导致:
- 文件监听失效(如 Flask 自动重载不工作)
- 权限问题
- I/O 性能大幅下降
最佳实践:把代码放在 Ubuntu 自己的文件系统里(如 /home/guyin/myproject)
4.2.迁移目录
打开powershell, 创建目录并进入
mkdir E:\ws2_space cd E:\ws2_space
导出ubuntu
wsl --export Ubuntu ubuntu.tar
注销(删除)当前发行版
wsl --unregister Ubuntu
从 tar 重新导入到 E 盘(注意路径)
wsl --import Ubuntu E:\ws2_space\Ubuntu ubuntu.tar --version 2
设置默认用户(否则会以 root 登录)
# 创建 /etc/wsl.conf 或运行: ubuntu config --default-user guyin # 如果是 Ubuntu 官方包
4.3.Windows文件资源管理器打开ubuntu系统目录
地址栏输入以下文本回车即可
\\wsl$\Ubuntu
5.安装docker
需进入wsl, powershell执行"wsl"
"cat /proc/1/comm" 命令输出了"systemd"说明systemd已启用
5.1.启动systemd(需要确认版本是否需要手动配置)
从 Windows 11 22H2 + WSL 内核 ≥ 5.10.67+ 开始,微软在部分版本中默认启用了 systemd 支持,无需手动配置
[wsl2]\nsystemd=true若要配置可能由于编码相关问题出错自行搜索即可.
- 按
Win + R,输入notepad回车 - 输入两行内容:
[wsl2] systemd=true
- 点击 文件 → 另存为
- 文件名:$env:USERPROFILE\.wslconfig
- 保存类型:所有文件 (.)
- 编码:选择 “UTF-8”(注意:Windows 记事本的 “UTF-8” 实际是 UTF-8 无 BOM,尽管名字没写清楚)
- 点“保存”
5.2.安装docker
安装依赖
# 安装依赖 sudo apt update sudo apt install -y ca-certificates curl gnupg lsb-release
下载GPG密钥(下载报错就多试几次)
# 1. 下载 GPG 密钥到临时文件 curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o docker.gpg # 2. 检查是否下载成功(应显示 "Public Key") file docker.gpg # 3. 转换为 gpg 格式并安装 sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg docker.gpg # 4. 删除临时文件 rm docker.gpg
添加仓库 & 安装 Docker
# 添加仓库 echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null # 更新包列表 sudo apt update # 安装 Docker Engine + Compose plugin sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
安装docker
# 安装 Docker Engine sudo apt update sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin # 将当前用户加入 docker 组(避免每次 sudo)(需要重启wsl2) sudo usermod -aG docker $USER
设置开机自启并设置镜像仓库地址
# 切换root
sudo su -
# 设置开机自启
systemctl enable docker.service
# 查看Docker状态
systemctl status docker
# 设置docekr镜像
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<'EOF'
{
"registry-mirrors": [
"https://docker.1ms.run",
"https://docker-0.unsee.tech",
"https://docker.m.daocloud.io"
],
"live-restore": true,
"features": { "buildkit": true }
}
EOF
# 重启docker
systemctl restart docker
# 查看docker状态
systemctl status docker
# 退出root
exit退出WSL2
exit
重启 WSL2
wsl --shutdown
然后重新打开 Ubuntu 终端。
wsl
验证
docker run --rm hello-world docker-compose --version
如下说明安装成功
ly@localhost:/mnt/e/ws2_space$ docker run --rm hello-world
docker compose version
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
17eec7bbc9d7: Pull complete
Digest: sha256:f7931603f70e13dbd844253370742c4fc4202d290c80442b2e68706d8f33ce26
Status: Downloaded newer image for hello-world:latest
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 version v2.40.3总结
到此这篇关于Windows10在wsl2上面安装docker的文章就介绍到这了,更多相关Windows10在wsl2安装docker内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
