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 等待重启就即可

相关推荐
打工仔折腾 AI9 小时前
Changedetection.io 网页变动监控:Docker 部署、通知配置与公网暴露实践
运维·人工智能·后端·python·docker·容器·性能优化
无奈笑天下9 小时前
银河麒麟上lsblk命令教程详解
linux·运维·服务器·数据库
fengkai454510 小时前
Prometheus+K8s弹性伸缩综合项目
运维·kubernetes·k8s·prometheus·hpa·vpa
分布式存储与RustFS12 小时前
镜像仓库的层别只堆本地盘:用 RustFS 给 Harbor 当 S3 后端
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
xbzb13 小时前
自建镜像仓库:registry 一条命令 + harbor 部署、授权、跨库同步与卸载
docker·harbor·镜像·仓库·同步·registry
雪落漂泊14 小时前
Liunux进程(七):程序控制篇(中)
linux·运维·windows
分布式存储与RustFS14 小时前
工作流脚本和依赖 JAR 散在各节点本地盘:用 DolphinScheduler 资源中心接到 RustFS
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
分布式存储与RustFS1 天前
群晖 NAS 被 C3RB3R 锁了:本地备份一起被加密,RustFS 对象锁为什么删不掉
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
真好啊又活了一天1 天前
服务器和虚机配置IPV6
运维·服务器·github
zixu_star1 天前
定时任务是什么?从日常自动化到 AI 应用,软件如何自动执行任务?
运维·人工智能·自动化