解决 WSL 中无法访问 registry-1.docker.io/v2/,无法用 docker 拉取 image
文章目录
无法拉取docker镜像
docker run hello-worldUnable to find image \'hello-world:latest\' locallydocker: Error response from daemon: Get \"https://registry-1.docker.io/v2/\": context deadline exceededRun \'docker run --help\' for more information
解决:
在 WSL 的 ~/.bashrc
加入如下内容:
export hostip=$(cat /etc/resolv.conf |grep -oP \'(?<=nameserver\\ ).*\')export https_proxy=\"http://${hostip}:7890\"export http_proxy=\"http://${hostip}:7890\"
刷新:source ~/.bashrc
编辑 /etc/resolv.conf
(该文件在WSL重启后会恢复默认配置)使用如下:
nameserver 8.8.8.8
保存后设置文件为不可变(防止被修改):
sudo chattr +i /etc/resolv.conf
同时宿主机器,使用 clash 开启虚拟网卡模式。
补充
迁移 WSL 位置
将 WSL 从 C 盘迁移到 D 盘。
当前版本
>wsl --list --verbose NAME STATE VERSION* Ubuntu Running 2
关闭 wsl、注销、导出为归档文件
wsl --shutdownwsl --export Ubuntu \"D:\\software\\WSL\\ubuntu_backup.tar\"wsl --unregister Ubuntu# 注销原先在C盘的 Ubuntu WSL 实例,删除原始文件并释放 C 盘空间。
导入并启动
wsl --import Ubuntu \"D:\\software\\WSL\\Ubuntu\" \"D:\\software\\WSL\\ubuntu_backup.tar\" --version 2wsl --list --verbosewsl
Install Docker
sudo apt update && sudo apt upgrade -ysudo apt install apt-transport-https ca-certificates curl gnupg lsb-releasecurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpgecho \"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable\" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/nullsudo apt updatesudo apt install docker-ce docker-ce-cli containerd.iosudo usermod -aG docker $USERsudo service docker startdocker --versiondocker run hello-world
sudo apt install docker-composedocker-compose --version