如何解决Docker网络请求问题:https://registry-1.docker.io/v2/ request canceled报错

报错

docker pull nginx
Using default tag: latest
Error response from daemon: Get "https://registry-1.docker.io/v2/": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

 方法一:给docker配置一个代理

Docker 代理

在执行docker pull时,是由守护进程dockerd来执行。因此,代理需要配在dockerd的环境中。而这个环境,则是受systemd所管控,因此实际是systemd的配置。

sudo mkdir -p /etc/systemd/system/docker.service.d
sudo touch /etc/systemd/system/docker.service.d/proxy.conf

在这个proxy.conf文件(可以是任意*.conf的形式)中,添加以下内容:

[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080/"
Environment="HTTPS_PROXY=http://proxy.example.com:8080/"
Environment="NO_PROXY=localhost,127.0.0.1,.example.com"
 【注】HTTP_PROXY 用于代理访问 http 请求,HTTPS_PROXY 用于代理访问 https 请求,如果想某个 IP域名不走代理则配置到 NO_PROXY中。
范例:
sudo mkdir -p /etc/systemd/system/docker.service.d

cat > /etc/systemd/system/docker.service.d/proxy.conf << EOF
[Service]
Environment="HTTP_PROXY=http://10.0.0.1:7890/"
Environment="HTTPS_PROXY=http://10.0.0.1:7890/"
Environment="NO_PROXY=localhost,127.0.0.1,.example.com"
EOF

systemctl daemon-reload ;systemctl restrat docker

### 访问Docker Hub注册表API 为了成功访问 Docker Hub 注册表 API `https://registry-1.docker.io/v2/`,需要确保网络连接稳定并配置好必要的认证信息。当遇到错误提示类似于 `net/http: request canceled` 时,这通常意味着请求被取消或超时,可能是由于网络不稳定或是未设置合适的镜像源造成的[^1]。 对于 Windows 用户来说,在尝试通过命令行工具或其他 HTTP 客户端发送请求之前,建议先验证本地环境是否能够正常解析域名以及是否有可用的互联网连接。如果确实存在上述提到的问题,则可以考虑调整 Docker 的配置文件来指定国内加速器地址作为镜像仓库的代理服务器之一: ```json { "registry-mirrors": [ "https://docker.211678.top", "https://docker.1panel.live", "https://hub.rat.dev", "https://docker.m.daocloud.io", "https://do.nark.eu.org", "https://dockerpull.com", "https://dockerproxy.cn", "https://docker.awsl9527.cn" ] } ``` 此 JSON 片段展示了如何修改 Docker 配置以利用多个中国境内的镜像站点提高下载速度和成功率[^2]。请注意,具体路径取决于操作系统版本;对于 Windows,默认情况下该文件位于 `%programdata%\docker\config\daemon.json` 中。 另外,要直接向 Docker Registry 发送 API 请求,还需要遵循官方文档中的指导完成身份验证过程。一般而言,可以通过获取 OAuth2 Token 或者 Basic Auth 凭证的方式来进行授权操作。下面是一个简单的 Python 脚本例子展示怎样使用 requests 库执行带认证的信息查询: ```python import base64 import json import requests def get_auth_token(username, password): auth_string = f'{username}:{password}' encoded_credentials = base64.b64encode(auth_string.encode('ascii')).decode('utf-8') headers = { 'Authorization': f'Basic {encoded_credentials}', 'Content-Type': 'application/json' } url = 'https://index.docker.io/v1/auth' response = requests.get(url=url, headers=headers) if response.status_code != 200: raise Exception(f"Failed to authenticate with status code {response.status_code}") token_data = response.json() return token_data['token'] if __name__ == '__main__': username = input("Enter your Docker ID:") password = input("Enter your Password:") try: token = get_auth_token(username=username, password=password) print(token) except Exception as e: print(e) ``` 这段代码实现了基本的身份验证流程,并打印出了获得的令牌。实际应用中应当妥善保管这些敏感数据而不是硬编码在脚本里。此外,考虑到安全性因素,推荐采用更安全的方法如 OAuth2 来代替明文传输用户名密码组合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值