解决 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
相关推荐
小小的木头人3 小时前
Docker 环境下迁移 containerd 数据目录:从 `/var/lib/containerd` 到 `/data/containerd`
linux·docker
Mr小林7 小时前
Docker 安装教程(在线 + 离线)
运维·docker·容器
java_logo8 小时前
Docker 部署 Milvus:轻松搭建高性能向量数据库平台
数据库·docker·私有化部署·milvus·向量数据库·rag·轩辕镜像
wdfk_prog12 小时前
VMware Ubuntu 虚拟机从 124 GB 压缩到 33 GB
运维·缓存·docker·容器
闲云野鹤在人间14 小时前
Docker入门|第3章 镜像详解
linux·网络·docker·容器·centos·云计算·php
典典分享指南15 小时前
AI 配图的排版规范
macos·docker·github·vim·visual studio
程序员老赵15 小时前
Docker 部署 DeepSeek Harness:轻松搭建本地 AI Agent 运行时平台
docker·agent·deepseek
闲云野鹤在人间15 小时前
Docker入门|第2章 容器架构详解
linux·运维·docker·容器·架构·云计算
wzq11_66615 小时前
Docker数据卷管理
运维·docker·容器
suweijie76815 小时前
Redis 7.0.2 Docker Compose 部署
redis·docker