Windows Docker 安装新路径(WSL 2 支持)

在 Windows 10/11 上安装 Docker 时,WSL 2(Windows Subsystem for Linux)已成为推荐的后端引擎。传统 Hyper-V 模式已被逐步淘汰,WSL 2 提供更高性能和原生 Linux 兼容性。

准备工作:

  • 确保系统为 Windows 10 2004 及以上版本或 Windows 11
  • 启用 BIOS 中的虚拟化技术(VT-x/AMD-V)
  • 至少 4GB 内存(建议 8GB+)

启用 WSL 2 功能

通过管理员权限运行 PowerShell 执行以下命令:

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

重启后设置 WSL 2 为默认版本:

wsl --set-default-version 2

安装 Linux 发行版

从 Microsoft Store 安装 Ubuntu 或 Debian 等发行版。安装完成后启动一次以完成初始化。

下载 Docker Desktop

访问 Docker 官网下载 Windows 版本安装包:

https://www.docker.com/products/docker-desktop/

安装时勾选以下选项:

  • Use WSL 2 instead of Hyper-V
  • Add shortcut to desktop

配置 Docker WSL 集成

打开 Docker Desktop 设置,进入 Resources → WSL Integration

  1. 启用已安装的 Linux 发行版(如 Ubuntu)
  2. 勾选 Enable integration with my default WSL distro
  3. 应用设置并重启 Docker

验证安装

在 PowerShell 或 WSL 终端中运行:

docker run --rm hello-world

成功运行后会出现 Docker 的欢迎信息。

性能优化建议

  • 将镜像存储位置迁移到非系统盘: 修改 Docker Desktop 设置中的 Advanced → Disk image location

  • 限制 WSL 内存使用(适用于低配设备): 在用户目录创建 .wslconfig 文件:

    [wsl2]
    memory=4GB
    processors=2
    

常见问题解决

问题1:WSL 2 安装失败

  • 检查 Windows 版本是否符合要求
  • 运行 wsl --update 手动更新内核

问题2:Docker 无法启动

  • 确保没有其他虚拟机软件(如 VirtualBox)冲突
  • 重置 Docker 到出厂设置:Troubleshoot → Reset to factory defaults

通过 WSL 2 后端运行的 Docker 可实现接近原生 Linux 的性能,特别适合需要同时使用 Windows 和 Linux 开发环境的场景。

更多推荐