WSL2 Docker Desktop配置优化及迁移D盘指南

Docker Engine 版本

v29.1.3

参考来源

参考官方文章 Advanced settings configuration in WSL

The wsl.conf and .wslconfig files are used to configure advanced settings in WSL that will be applied on start up of the WSL VM. wsl.conf is used to apply settings on a per WSL distro basis, and .wslconfig is used to apply global settings to WSL.

翻译:

wsl.conf 和 .wslconfig 文件用于配置 WSL 的高级设置,这些设置将在 WSL 虚拟机启动时生效。wsl.conf 用于按 WSL 发行版逐个应用配置,而 .wslconfig 则用于为整个 WSL 系统应用全局设置。

.wslconfig

Configure global settings with .wslconfig across all installed distributions running on WSL.

The .wslconfig file does not exist by default. It must be created and stored in your %UserProfile% directory to apply these configuration settings.

Used to configure settings globally across all installed Linux distributions running as the WSL 2 version.

Can be used only for distributions run by WSL 2. Distributions running as WSL 1 will not be affected by this configuration as they are not running as a virtual machine.

To get to your %UserProfile% directory, in PowerShell, use cd ~ to access your home directory (which is typically your user profile, C:\Users<UserName>) or you can open Windows File Explorer and enter %UserProfile% in the address bar. The directory path should look something like: C:\Users<UserName>.wslconfig.

翻译:

通过.wslconfig文件为所有已安装的WSL发行版配置全局设置

默认情况下不存在.wslconfig文件。必须创建该文件并将其保存在%UserProfile%目录中才能使配置生效

该文件用于为所有以WSL 2版本运行的Linux发行版配置全局设置

仅适用于WSL 2运行的发行版。以WSL 1运行的发行版不受此配置影响,因为它们不以虚拟机方式运行

要进入%UserProfile%目录:在PowerShell中使用cd ~命令进入主目录(通常就是用户配置文件目录C:\Users<用户名>),或者可以在Windows文件资源管理器的地址栏中输入%UserProfile%。目录路径应类似于:C:\Users<用户名>.wslconfig

创建配置文件

进入%USERPROFILE%

进入创建文件 .wslconfig

.wslconfig file 模板

按需配置,不需要最好保留默认

bash 复制代码
# 以下设置适用于所有运行在WSL 2上的Linux发行版
[wsl2]

# 限制WSL 2虚拟机内存使用上限为4GB,支持GB或MB为单位(需为整数)
memory=4GB

# 设置虚拟机使用2个虚拟CPU核心
processors=2

# 指定自定义Linux内核路径(默认内核参考:https://github.com/microsoft/WSL2-Linux-Kernel)
kernel=C:\\temp\\myCustomKernel

# 指定自定义内核模块的VHDX文件路径
kernelModules=C:\\temp\\modules.vhdx

# 设置附加内核参数(本例启用对CentOS 6等旧版镜像的支持)
kernelCommandLine = vsyscall=emulate

# 设置交换空间大小为8GB(默认为可用内存的25%)
swap=8GB

# 自定义交换文件存储路径(默认路径:%UserProfile%\AppData\Local\Temp\swap.vhdx)
swapfile=C:\\temp\\wsl-swap.vhdx

# 启用默认网络绑定,将WSL 2的localhost与Windows本地主机连通(当networkingMode=mirrored时此设置无效)
localhostforwarding=true

# 禁用嵌套虚拟化功能
nestedVirtualization=false

# 开启调试控制台,启动WSL 2发行版时显示dmesg日志
debugConsole=true

# 设置最大崩溃转储文件保留数量(默认为5个)
maxCrashDumpCount=10

# 实验性功能配置区
[experimental]
# 启用稀疏VHD优化(节省磁盘空间)
sparseVhd=true

无注释版,有时候中文会导致奇怪的错误

bash 复制代码
[wsl2]
memory=4GB
processors=2
kernel=C:\\temp\\myCustomKernel
kernelModules=C:\\temp\\modules.vhdx
kernelCommandLine = vsyscall=emulate
swap=8GB
swapfile=C:\\temp\\wsl-swap.vhdx
localhostforwarding=true
nestedVirtualization=false
debugConsole=true
maxCrashDumpCount=10

[experimental]
sparseVhd=true

配置完成后就右键任务栏的dockerdesktop,选择Restart

重启成功,显示对应的是我最新的配置

memory=4GB

processors=2

迁移到D盘

点击设置 👉 resource栏 👉 Advance栏 👉更改Disk image location 👉点击Browse选择一个文件夹 👉 Apply & Restart 等待重启就即可

相关推荐
大树883 小时前
液冷系统的真正瓶颈,藏在那层不到1毫米的材料里
大数据·运维·服务器·人工智能·ai
yindeshuiketang3 小时前
企业FDE架构方法论到实战指南从想法到商业变现:需求·设计·技术·测试·运维·交付·变现-小红书&抖音 AI马教授 职场启航宝
运维·人工智能·架构
网硕互联的小客服3 小时前
没有公网IP,如何让外网访问内网服务器?
运维·服务器
布裘3 小时前
【银河麒麟】服务器系统开机黑屏故障排查与修复
linux·运维·服务器·银河麒麟·无法启动
凌风的跨境分享3 小时前
Temu店群运维提效:定时策略自动化任务全场景实操指南
大数据·运维·前端·人工智能·架构·自动化
Zenova EdgeOS3 小时前
Linux iotop:工业边缘 I/O 高级分析实战
linux·运维·服务器
Black蜡笔小新3 小时前
运维提效!视频汇聚平台EasyCVR视频质量诊断,掉线花屏自动预警
运维·安全·音视频
又見山4 小时前
Codex 实战:用 AI 写运维脚本
运维·人工智能
Elastic 中国社区官方博客4 小时前
Elasticsearch 向量数据库:几分钟内完成部署,以经济高效的方式扩展至数千亿规模
大数据·运维·数据库·elasticsearch·搜索引擎·ai·全文检索
赵民勇4 小时前
resolvectl命令详解
linux·运维