美文网首页
Docker 拉取镜像失败处理

Docker 拉取镜像失败处理

作者: yanlong107 | 来源:发表于2021-12-02 18:12 被阅读0次

    问题

    如果母机的网络环境是需要配置代理才能够访问互联网的话,那安装docker后,去拉取镜像大概率会失败。
    类似如下的错误:

    guest@guest-PC:~$ docker pull ubuntu:latest
    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)
    

    问题原因

    母机通过代理访问外网时,一般在命令行中通过 export 来设置代理

    export http_proxy=http://*****:8080
    export https_proxy=http://*****:8080
    

    而 docker拉取镜像是通过 docker daemon 服务完成,docker daemon 服务是在另一个进程中,不会读取当前shell 命令行的代理,所以 docker pull 会失败。

    官方文档

    解决方案

    1、在 /etc/systemd/system/docker.service.d/http-proxy.conf 配置文件中添加代理信息
    2、重启docker服务

    具体操作如下:

    1. 创建 dockerd 相关的 systemd 目录,这个目录下的配置将覆盖 dockerd 的默认配置
    $ sudo mkdir -p /etc/systemd/system/docker.service.d
    
    1. 新建配置文件 /etc/systemd/system/docker.service.d/http-proxy.conf 内容如下:
    [Service]
    Environment="HTTP_PROXY=http://*****.com:80"
    Environment="HTTPS_PROXY=https://*****.com:443"
    

    tips: 如果文件已经存在,可直接修改文件内容,添加相关代理设置

    如果有不需要代理的地址,可添加 NO_PROXY 配置:

    [Service]
    Environment="HTTP_PROXY=http://*****.com:80"
    Environment="HTTPS_PROXY=https://*****.com:443"
    Environment="NO_PROXY=your-registry.com,*.*.*.*,*.example.com"
    

    多个 NO_PROXY 变量的值用逗号分隔,而且可以使用通配符(),极端情况下,如果 NO_PROXY=,那么所有请求都将不通过代理服务器

    1. 重新加载配置文件,重启 dockerd 服务
    $ sudo systemctl daemon-reload
    $ sudo systemctl restart docker
    
    1. docker info 查看配置结果


      image.png

    结论:

    docker 镜像由 docker daemon 管理,所以不能用修改 shell 环境变量的方法使用代理服务,而是从 systemd 角度设置环境变量

    参考资料

    1、https://docs.docker.com/config/daemon/systemd/#httphttps-proxy
    2、https://www.lfhacks.com/tech/pull-docker-images-behind-proxy/

    相关文章

      网友评论

          本文标题:Docker 拉取镜像失败处理

          本文链接:https://www.haomeiwen.com/subject/shtuxrtx.html