WSL+Windows Docker:10 分钟优雅配置,开发环境搭建提速
·
WSL 与 Windows Docker 集成配置
启用 WSL 功能
在 PowerShell 管理员模式下运行以下命令,启用 WSL 和虚拟机平台功能:
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 Desktop
从 Docker 官网下载 Windows 版本安装包,安装时勾选以下选项:
- Use WSL 2 based engine
- Enable integration with my default WSL distro
配置 WSL 集成
打开 Docker Desktop 设置,进入 Resources > WSL Integration,启用已安装的 WSL 发行版,确保 Docker 命令可在 WSL 中直接调用。
验证环境
检查 WSL 版本
在 PowerShell 中运行以下命令确认 WSL 版本为 2:
wsl -l -v
测试 Docker 命令
在 WSL 终端中运行以下命令验证 Docker 是否正常工作:
docker run hello-world
性能优化建议
调整资源分配
在 Docker Desktop 设置中,根据主机配置调整 CPU、内存和磁盘资源限制,避免资源争用。
关闭 Windows 防火墙
若遇到网络问题,临时关闭 Windows Defender 防火墙测试是否为干扰因素。
使用阿里云镜像加速
在 Docker Desktop 配置文件中添加国内镜像源提升拉取速度:
"registry-mirrors": ["https://<your-id>.mirror.aliyuncs.com"]
常见问题解决
WSL 2 无法启动
确保 BIOS 中启用虚拟化(VT-x/AMD-V),Hyper-V 和 Windows 沙箱功能已关闭。
Docker 命令权限不足
在 WSL 中执行以下命令将用户加入 docker 组:
sudo usermod -aG docker $USER
端口冲突
检查 Windows 和 WSL 中是否有进程占用相同端口,使用 netstat -ano 定位冲突进程。
更多推荐

所有评论(0)