解决 WSL 中无法访问 registry-1.docker.io/v2/,无法用 docker 拉取 image

文章目录

无法拉取docker镜像

bash 复制代码
docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": context deadline exceeded

Run 'docker run --help' for more information

解决:

在 WSL 的 ~/.bashrc 加入如下内容:

bash 复制代码
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重启后会恢复默认配置)使用如下:

bash 复制代码
nameserver 8.8.8.8

保存后设置文件为不可变(防止被修改):

bash 复制代码
sudo chattr +i /etc/resolv.conf

同时宿主机器 ,使用 clash 开启虚拟网卡模式。


补充

迁移 WSL 位置

将 WSL 从 C 盘迁移到 D 盘。

当前版本

bash 复制代码
>wsl --list --verbose
  NAME      STATE           VERSION
* Ubuntu    Running         2

关闭 wsl、注销、导出为归档文件

bash 复制代码
wsl --shutdown
wsl --export Ubuntu "D:\software\WSL\ubuntu_backup.tar"
wsl --unregister Ubuntu
# 注销原先在C盘的 Ubuntu WSL 实例,删除原始文件并释放 C 盘空间。

导入并启动

bash 复制代码
wsl --import Ubuntu "D:\software\WSL\Ubuntu" "D:\software\WSL\ubuntu_backup.tar" --version 2

wsl --list --verbose
wsl

Install Docker

bash 复制代码
sudo apt update && sudo apt upgrade -y
sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "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/null

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
sudo usermod -aG docker $USER
sudo service docker start

docker --version
docker run hello-world
bash 复制代码
sudo apt install docker-compose
docker-compose --version
相关推荐
海的透彻17 分钟前
如何让docker容器启动时用自定义的用户
java·docker·dubbo
程序员清风1 小时前
LibreChat 实战:用 Docker 搭建可扩展的多模型对话平台
运维·docker·容器
Web3&Basketball2 小时前
从0到1落地AI Agent沙箱隔离Docker实战:踩坑全记录
运维·docker·容器·agent·容器安全·沙箱隔离
bluebonnet273 小时前
【docker】使用regctl免安装docker下载镜像
运维·docker·容器
Java开发追求者20 小时前
windows快速安装docker
windows·docker·安装·1024程序员节
骑着蜗牛撵大象3271 天前
飞牛fnOS部署Damselfly:Compose编排、照片目录与7100固定访问
java·docker·rpc·dubbo·fnos·照片管理·damselfly
一掬净土1 天前
kubelet 报错如下:docker does not support reopening container log files
docker·云原生·kubelet·logfiles
TTATTC1 天前
为什么 Python 做 Agent 总在并发中崩溃?拆解 Blockcell 在 Rust 下的 Host-Cell 隔离与自愈状态机
docker·开源
骑着蜗牛撵大象3271 天前
N1 小盒子撑起 AI 接口中转层:New-API 接管本地 Ollama,用 Token 分发模型能力
docker·llama·1024程序员节·api网关·飞牛nas·new-api
艸肅1 天前
Docker 国内镜像源配置
linux·ubuntu·docker·1024程序员节