windows wsl2-06-docker hello world

发布于:2025-07-20 ⋅ 阅读:(12) ⋅ 点赞:(0)

hello-world 例子

就像其他任何一门语言一样,我们来体验 docker 的 hello world

$ docker run hello-world

但是报错

:~$ docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": dial tcp 208.101.60.87:443: i/o timeout.
See 'docker run --help'.

原因

网络问题,发现 WSL 中网络不通

$ curl -v https://registry-1.docker.io/v2/
*   Trying 199.59.149.202:443...
*   Trying 2001::9df0:232:443...
* Immediate connect fail for 2001::9df0:232: Network is unreachable
* connect to 199.59.149.202 port 443 failed: Connection timed out
* Failed to connect to registry-1.docker.io port 443 after 133350 ms: Connection timed out
* Closing connection 0
curl: (28) Failed to connect to registry-1.docker.io port 443 after 133350 ms: Connection timed out

解决办法

这和之前的 Docker 报错一致,问题根源在网络连通性,而不是链接本身。

1. 使用国内镜像加速(最快解决)

在 WSL 中执行:

sudo tee /etc/docker/daemon.json <<'EOF'
{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "https://registry.docker-cn.com"
  ]
}
EOF

然后重启 Docker:

sudo systemctl restart docker  # 若你启用了 systemd
# 或
sudo service docker restart    # 若未启用 systemd

再试:

docker run hello-world

发现还是不行,依然报错

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.

不过在 WSL 中 ping 一下百度,网络正常

$ ping baidu.com
PING baidu.com (182.61.244.181) 56(84) bytes of data.
64 bytes from 182.61.244.181 (182.61.244.181): icmp_seq=1 ttl=51 time=12.0 ms
64 bytes from 182.61.244.181 (182.61.244.181): icmp_seq=2 ttl=51 time=54.8 ms
64 bytes from 182.61.244.181 (182.61.244.181): icmp_seq=3 ttl=51 time=35.9 ms
64 bytes from 182.61.244.181 (182.61.244.181): icmp_seq=4 ttl=51 time=90.4 ms

确认配置+添加DNS解析

确认配置

$ cat  /etc/docker/daemon.json
{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "https://registry.docker-cn.com"
  ]
}

dns

$ cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
# nameserver 172.23.224.1
nameserver 114.114.114.114

可用 ip

dig @114.114.114.114 registry-1.docker.io 找到可用IP

结果很少,感觉很奇怪。

$ dig @114.114.114.114 registry-1.docker.io +short
168.143.162.58

修改 host 文件

echo "168.143.162.58 registry-1.docker.io" | sudo tee -a /etc/hosts

重启

$ sudo systemctl restart docker

确认

docker run hello-world

发现还是不行,真的麻烦。


网站公告

今日签到

点亮在社区的每一天
去签到