解决 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
相关推荐
小小洋洋7 小时前
OpenWrt 从U盘迁移到内置 eMMC,并完成扩容与 Docker 安装
java·docker·eureka
Eloudy7 小时前
docker build 与 docker buildx build 的应用场景
docker·构建
瞬间&永恒~8 小时前
【Docker】(三)联合文件系统UnionFS + Overlay2
docker·云原生·容器
数据知道10 小时前
主机入侵检测(HIDS):OSSEC / Wazuh 部署与规则编写
安全·网络安全·docker
逐光老顽童11 小时前
第 5 章:搞懂 K8s Service:从 ClusterIP 到负载均衡
docker·云原生·容器
数智工坊12 小时前
Conda 环境 Docker 标准化迁移完整方案,适配深度学习 GPU 训练
深度学习·docker·conda
花生了什么事o13 小时前
Docker 部署 SpringBoot:从镜像构建到服务器运行
服务器·spring boot·docker
Eloudy13 小时前
国内加速 docker pull 下载 docker images,特别是 nvidia 的cuda image
docker·gpu·rdma
Chief_fly13 小时前
ARM 麒麟系统服务器构建docker镜像
运维·服务器·docker
yj_xqj1 天前
Docker 基础应用与介绍
运维·docker·容器