WSL2 Docker Desktop配置优化及迁移D盘指南 Docker Engine 版本v29.1.3参考来源参考官方文章 Advanced settings configuration in WSLThe 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 系统应用全局设置。.wslconfigConfigure 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:\UsersUserName) or you can open Windows File Explorer and enter %UserProfile% in the address bar. The directory path should look something like: C:\UsersUserName.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 模板按需配置不需要最好保留默认# 以下设置适用于所有运行在WSL 2上的Linux发行版[wsl2]# 限制WSL 2虚拟机内存使用上限为4GB支持GB或MB为单位需为整数memory4GB# 设置虚拟机使用2个虚拟CPU核心processors2# 指定自定义Linux内核路径默认内核参考https://github.com/microsoft/WSL2-Linux-KernelkernelC:\\temp\\myCustomKernel# 指定自定义内核模块的VHDX文件路径kernelModulesC:\\temp\\modules.vhdx# 设置附加内核参数本例启用对CentOS 6等旧版镜像的支持kernelCommandLinevsyscallemulate# 设置交换空间大小为8GB默认为可用内存的25%swap8GB# 自定义交换文件存储路径默认路径%UserProfile%\AppData\Local\Temp\swap.vhdxswapfileC:\\temp\\wsl-swap.vhdx# 启用默认网络绑定将WSL 2的localhost与Windows本地主机连通当networkingModemirrored时此设置无效localhostforwardingtrue# 禁用嵌套虚拟化功能nestedVirtualizationfalse# 开启调试控制台启动WSL 2发行版时显示dmesg日志debugConsoletrue# 设置最大崩溃转储文件保留数量默认为5个maxCrashDumpCount10# 实验性功能配置区[experimental]# 启用稀疏VHD优化节省磁盘空间sparseVhdtrue无注释版有时候中文会导致奇怪的错误[wsl2]memory4GBprocessors2kernelC:\\temp\\myCustomKernelkernelModulesC:\\temp\\modules.vhdx kernelCommandLinevsyscallemulateswap8GBswapfileC:\\temp\\wsl-swap.vhdxlocalhostforwardingtruenestedVirtualizationfalsedebugConsoletruemaxCrashDumpCount10[experimental]sparseVhdtrue配置完成后就右键任务栏的dockerdesktop选择Restart重启成功显示对应的是我最新的配置memory4GBprocessors2迁移到D盘点击设置 resource栏 Advance栏 更改Disk image location点击Browse选择一个文件夹 Apply Restart 等待重启就即可