V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zypdominate1
V2EX  ›  Docker

WSL 中无法访问 registry-1.docker.io/v2/,没法用 docker 拉取 image,试了很多方法都不行,累了

  •  
  •   zypdominate1 · 44 天前 · 2057 次点击
    这是一个创建于 44 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在 windows11 中安装了 WSL 2 ( Ubuntu ),安装了 docker 后,死活没法 pull image 。

    docker-compose up
    Pulling db (mysql:8.0)...
    ERROR: Get "https://registry-1.docker.io/v2/": context deadline exceeded
    

    尝试过的方法:

    1. 修改 /etc/docker/daemon.json ,添加阿里云镜像源
    2. 使用 ipv4
    3. 修改 /etc/resolv.conf ,使用 nameserver 8.8.8.8 nameserver 1.1.1.1

    麻了,有没大佬知道应该怎么解决这个问题呀。

    第 1 条附言  ·  42 天前

    最终在众多留言大佬的帮助下,终于解决了这个问题。

    在 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,使用如下:

    nameserver 8.8.8.8
    

    另外,宿主机(我的window11)使用 clash verge,开启虚拟网卡模式,用的是默认配置。(一开始我还不懂网友们说的 TUN 是什么,直到点击虚拟网卡模式的设置,才看到)

    很坑的是,这个 /etc/resolv.conf 每次重启 WSL 后又会恢复默认配置……

    26 条回复    2025-08-27 22:49:02 +08:00
    8820670
        2
    8820670  
       44 天前 via Android
    op 是很久没有用 docker 了吗 dockerhub 以及大的镜像站都被墙很久了
    zypdominate1
        3
    zypdominate1  
    OP
       44 天前
    @dcsuibian 还是不行,不知道是不是我配置的有问题,我是在自己个人电脑上使用的。代理的地址要怎么配置才好,对代理不太懂。
    ```
    cat http-proxy.conf

    [Service]
    Environment="HTTP_PROXY=http://192.168.1.100:8080" # HTTP 代理
    Environment="HTTPS_PROXY=http://192.168.1.100:8080" # HTTPS 代理
    Environment="NO_PROXY=localhost,127.0.0.1,10.0.0.0/8,.internal" # 直连的地址
    mag@Mag:/etc/systemd/system/docker.service.d$ cd -
    /home/mag/tools/mysql-docker

    mag@Mag:~/tools/mysql-docker$ sudo systemctl daemon-reload
    mag@Mag:~/tools/mysql-docker$ sudo systemctl restart docker

    mag@Mag:~/tools/mysql-docker$ docker-compose up
    Pulling db (mysql:8.0)...
    ERROR: Get "https://registry-1.docker.io/v2/": proxyconnect tcp: dial tcp 192.168.1.100:8080: connect: no route to host


    docker run hello-world
    Unable to find image 'hello-world:latest' locally
    docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": proxyconnect tcp: dial tcp 192.168.1.100:8080: connect: no route to host
    ```
    capric
        4
    capric  
       44 天前
    代理要用 wsl 宿主的 ip ,不是 192 的 ip ,可以用 ip route show | grep -i default | awk '{ print $3}'获取,参照文档 https://learn.microsoft.com/en-us/windows/wsl/networking
    zed1018
        5
    zed1018  
       44 天前
    代理如果是在 win 上,直接用 localhost+端口就行了,可以先用 nc 测试一下
    dcsuibian
        6
    dcsuibian  
       44 天前   ❤️ 1
    @zypdominate1 如果你用的是 clash 的话,那端口一般是 789x 什么的。要开启允许局域网连接,
    Lemonadeccc
        7
    Lemonadeccc  
       44 天前
    如果是在自己 win 上,梯子开 tun 就行
    bigshawn
        8
    bigshawn  
       44 天前
    建议搞了透明代理,省的配置这个配置那个。
    zypdominate1
        9
    zypdominate1  
    OP
       44 天前
    @Lemonadeccc win 上的梯子开代理,在 wsl 中也没法 pull image 。总之,都试过了,累了
    zypdominate1
        10
    zypdominate1  
    OP
       44 天前
    @capric 貌似不支持代理?

    wsl -d Ubuntu hostname -I
    wsl: 检测到 localhost 代理配置,但未镜像到 WSL 。NAT 模式下的 WSL 不支持 localhost 代理。
    172.20.107.81 172.17.0.1 172.18.0.1
    tt0411
        11
    tt0411  
       44 天前
    试试国内镜像加速吧, 网上有很多, 比如 https://cloud.tencent.com/developer/article/2485043
    daisyfloor
        12
    daisyfloor  
       44 天前   ❤️ 1
    - wsl2 配置里网络改为 mirror 模式: networkingMode=mirrored
    - windows 上代理软件使用 tun 模式: 并开启严格路由 strict-route: true , 且 tun 的 mtu 改为 1500

    这样配置后,在可靠的 TUN 网络下,wsl2 自己(如 apt update 或者 curl )和 docker 引擎(如镜像拉取,容器网络请求)都可以正常无感知使用代理。
    daimiaopeng
        13
    daimiaopeng  
       44 天前
    v2rayn 开 tun 模式亲测可行
    Xheldon
        14
    Xheldon  
       44 天前
    你需要修改 wslconfig ,网上有教程,后两项关了就可
    YaakovZiv
        15
    YaakovZiv  
       44 天前
    Linux 系统使用 docker 可以直接白嫖免费的,谷歌搜索关键词"xuanyuan docker"或者"1ms docker"
    hefish
        16
    hefish  
       44 天前
    我是直接在路由器上开了个梯子。。。
    如果是外面的服务器,国内的,就开个代理指向家里的梯子,如果是国外的就直接 docker pull
    laminux29
        17
    laminux29  
       44 天前
    1. Docker 是原生于 Linux 的系统,不要在 Windows 上用 docker ,不然发生任何问题都不奇怪。推荐使用 Debian 12 系统。

    2.请不要使用机场,机场是合租性质,不稳定。一定要买带有 CN2 线路优化的大厂海外 VPS ,比机场稳太多。

    3.使用诸如 xShell 之类的 SSH 工具连接 VPS ,并启用 socks 隧道功能。然后去 github 搜 socks 转 http 的小程序,把 socks 隧道转换为 http 隧道。最后编辑 docker 的配置文件:
    /etc/systemd/system/multi-user.target.wants/docker.service

    在 [Service] 节点下加入:
    Environment="HTTP_PROXY=http://socks 转 http 程序所在服务器的 IP:端口/"
    Environment="HTTPS_PROXY=http://socks 转 http 程序所在服务器的 IP:端口/"
    Environment="NO_PROXY=localhost,10.0.0.0/8,127.0.0.0/8,172.16.0.0/16,192.168.0.0/16"

    最后
    systemctl daemon-reload
    systemctl restart docker
    重启 docker 服务就行了。
    triptipstop
        18
    triptipstop  
       44 天前
    用 docker-desktop 很舒服 就是得把 扩展 插件 AI 什么的全关了 不然一升级就坑
    SenLief
        19
    SenLief  
       43 天前
    docker run hello-world
    Unable to find image 'hello-world:latest' locally
    docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": proxyconnect tcp: dial tcp 192.168.1.100:8080: connect: no route to host

    没有连上代理,建议直接宿主机 tun
    fakecoder
        20
    fakecoder  
       43 天前
    我是 wsl2 安装了 clash,op 可以试试...
    chaleaochexist
        21
    chaleaochexist  
       43 天前
    1. wsl 配置镜像网络
    2. 科学上网, 虚拟网卡模式

    如果是代理模式, 那就走代理.
    zaunist
        22
    zaunist  
       43 天前
    https://note.qidong.name/2020/05/docker-proxy/
    楼主看下这篇文章吧,docker 在不同使用条件下的代理配置,根据你的信息来看,你只是想拉取镜像,那么我的建议是找个镜像站,添加到 daemon.json 里就行了,至于什么 NAT 、TUN 啥的,都不必折腾,最省事的办法就是找个镜像站。
    gray0
        23
    gray0  
       43 天前   ❤️ 1
    在 wsl2 中,使用主机的 clash 代理,在本地~/.zshrc 或 ~/.bashrc 加入如下内容:
    export hostip=$(cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*')
    export https_proxy="http://${hostip}:7890"
    export http_proxy="http://${hostip}:7890"
    szguan
        24
    szguan  
       43 天前
    ## .wslconfig 中:
    ```
    networkingMode=mirrored
    autoProxy=true
    ```

    ## wsl2 中 /etc/wsl.conf
    ```
    [network]
    generateResolvConf = false
    ```

    ## wsl2 中 /etc/resolv.conf
    ```
    nameserver 8.8.8.8
    ```

    ## 添加代理设置:
    sudo nano ~/.bashrc

    ```
    # 使用主机地址( WSL2 镜像网络)
    export hostip=127.0.0.1
    export hostport=7890 (替换为主机代理端口)
    # HTTP/HTTPS 代理(常用程序使用)
    export http_proxy="http://${hostip}:${hostport}"
    export https_proxy="http://${hostip}:${hostport}"
    export HTTP_PROXY="$http_proxy"
    export HTTPS_PROXY="$https_proxy"
    # SOCKS5 代理(部分支持 SOCKS5 的工具可使用)
    export all_proxy="socks5h://${hostip}:${hostport}"
    export ALL_PROXY="$all_proxy"
    # 排除内网和本地地址不走代理
    export no_proxy="localhost.0.0.1,::1,*.local.*.*.168.*"
    export NO_PROXY="$no_proxy"

    # proxychains4 命令别名(适用于需要强制 SOCKS 的程序)
    alias pwget="proxychains4 wget"
    alias pcurl="proxychains4 curl"
    ```

    ## 使配置生效
    `source ~/.bashrc`
    zypdominate1
        25
    zypdominate1  
    OP
       42 天前
    @gray0 卧槽,大佬,你太牛逼了,来在深夜的感慨,本来都准备放弃了。感谢~

    ```
    docker run hello-world
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    e6590344b1a5: Pull complete
    Digest: sha256:ec153840d1e635ac434fab5e377081f17e0e15afab27beb3f726c3265039cfff
    Status: Downloaded newer image for hello-world:latest

    Hello from Docker!
    This message shows that your installation appears to be working correctly.

    To generate this message, Docker took the following steps:
    1. The Docker client contacted the Docker daemon.
    2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
    3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
    4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

    To try something more ambitious, you can run an Ubuntu container with:
    $ docker run -it ubuntu bash

    Share images, automate workflows, and more with a free Docker ID:
    https://hub.docker.com/

    For more examples and ideas, visit:
    https://docs.docker.com/get-started/
    ```
    SeanChang
        26
    SeanChang  
    PRO
       5 天前
    @tt0411 赞~好用~

    群主试试轩辕镜像 docker 加速~https://xuanyuan.cloud
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2876 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 13:50 · PVG 21:50 · LAX 06:50 · JFK 09:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.