因为最近需要在windows上安装docker,所以特地去了解了怎么开启wsl,怎么配置,怎么设置docker desktop。看了许多教程,发现都没办法解决我的问题(win11家庭+24H2+AMD R7 8845hs),前前后后加起来快一天了,最后终于完美解决了,下面就说下解决方法和踩的坑。
全部流程
- 控制面板->程序->启用或关闭Windows功能->勾选Virtual Machine Platform和媒体功能下的适用于Linux的Windows 子系统(其实没必要使用Hyper-V,之前我还以为要和WSL一起,还通过bat开启了Hyper-V功能,家庭版本身没有这个功能)
ps:我的系统之前就开启了虚拟化,在任务管理器->性能中可以看到,所以如何开启可以自行搜索。 - 直接通过终端powershell运行
wsl --install或者wsl --update,等他更新完就是wsl2了(需要提前设置代理环境,不然龟速) - 去Windows商店下载你想运行的系统环境,我选的ubuntu22.04,算是最稳定的了。然后运行
wsl --set-default-version 2设置WSL默认版本为2 - 因为默认安装c盘,所以可以先找到磁盘文件,位置类似:
C:\Users\***\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx,然后
# 关闭虚拟机
wsl --shutdown
# 导出Ubuntu-22.04文件(注:这里的Ubuntu-22.04即wsl -l -v中看到的虚拟机名称)
wsl --export Ubuntu-22.04 d:\***\WSL2Ubuntu22.04.bak
# 注销原来的虚拟机
wsl --unregister Ubuntu-22.04
# 重新导入(注:d:\***\wsl目录要存在)
wsl --import Ubuntu-22.04 d:\***\wsl d:\***\WSL2Ubuntu22.04.bak --version 2
- 然后就是打开ubuntu创建账户和密码了,接着就到了马上抓狂的地方了,先指定位置安装docker desktop,先创建Docker文件夹
"D:\***\Docker Desktop Installer.exe" install --installation-dir="D:***\Docker" - 安装完成后,管理员方式打开docker,先在右上角齿轮->Resources->-Advanced把container位置修改在其他盘,然后在Proxies打开Manual proxy configuration,输入你的代理地址
http://127.0.0.1:***
http://127.0.0.1:***
localhost,127.0.0.1,*.local,kubernetes.docker.internal
- 关闭应用,随后直接在C:\Users*.docker**修改daemon.json
{
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"proxies": {
"default": {
"httpProxy": "http://127.0.0.1:10808",
"httpsProxy": "http://127.0.0.1:10808",
"noProxy": "localhost,127.0.0.1,*.local,kubernetes.docker.internal"
}
}
}
- 先设置wsl网络模式,直接在C:\Users*.wslconfig**修改(没有直接新建)
[wsl2]
memory=*GB #内存的一半就行
swap=*GB #上面的一半
processors=* #留4个线程给windows就行
networkingMode=mirrored #这个比较方便
firewall=true
dnsTunneling=true
autoProxy=false #很重要,之前就是看了各种教程设置为true导致docker环境被覆盖,一直出错,找不到原因
[experimental]
hostAddressLoopback=true
sparseVhd=true
autoMemoryReclaim=gradual
- 然后重新打开docker就可以啦(到这一步踩了无数坑,浪费了很多时间,还设置了各种防火墙规则,其实都是Hyper-V的)
- 为wsl也设置代理,这一步就熟悉的不能再熟悉了
nano ~/.bashrc
#直接在最下方输入
PROXY_URL="http://127.0.0.1:***"
export HTTP_PROXY="${PROXY_URL}"
export HTTPS_PROXY="${PROXY_URL}"
export http_proxy="${PROXY_URL}"
export https_proxy="${PROXY_URL}"
export NO_PROXY="localhost,127.0.0.1,*.local,kubernetes.docker.internal"
export no_proxy="localhost,127.0.0.1,*.local,kubernetes.docker.internal"
echo "Proxy environment variables set."
重启wsl(命令在前面),测试curl -v ip.sb就可以验证啦。
这里面最坑的就是
自动代理
将本地http环境自动应用于wsl,听起来是很不错,但是目前不稳定,会导致docker无法正常拉取镜像,一直提示HTTP 500,proxy reject之类的,导致我80%以上时间都在解决这个问题,虽然期间设置了镜像源仓库可以解决,但是我就是想走自己的环境。所以我们要
关闭它,分别为docker和wsl设置环境
发个帖留做记录,顺便为佬友避避坑