美文网首页容器Docker
配置docker使用 proxy

配置docker使用 proxy

作者: 蟹蟹宁 | 来源:发表于2021-09-23 21:31 被阅读0次

    前言

    docker使用proxy分两种情况:

    • docker client希望使用代理,也就是在执行docker pull、docker push等操作时通过代理来访问镜像仓库
    • 容器实例希望使用代理,也就是在容器内部希望通过代理来访问网络

    两者的配置显然是不一样,各自的官方文档地址:

    当使用wsl2时:
    WSL 2 Docker Behind Proxy.

    一、配置client 代理(常规安装方式)

    1、创建配置文件

    sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf
    

    2、添加配置

    [Service]
    Environment="HTTP_PROXY=http://211.69.198.232:8118"
    Environment="HTTPS_PROXY=http://211.69.198.232:8118"
    Environment="NO_PROXY=localhost,127.0.0.1,docker-registry.example.com,.corp,211.69.198.232"
    

    3、重启容器

    sudo systemctl daemon-reload
    sudo systemctl restart docker
    

    二、配置容器实例代理

    方式一,配置文件(仅支持 Docker 版本 >= 17.07)

    1、创建配置文件

    vim  ~/.docker/config.json
    

    2、添加配置信息

    {
     "proxies":
     {
       "default":
       {
         "httpProxy": "http://211.69.198.232:8118",
         "httpsProxy": "http://211.69.198.232:8118",
         "noProxy": "*.test.example.com,.example2.com,127.0.0.0/8,211.69.198.232"
       }
     }
    }
    
    方式二,手动添加环境变量(仅支持 Docker 版本 >= 17.07)

    通过 --env 在启动容器时,添加对应的环境变量:

    Variable Dockerfile example docker run example
    HTTP_PROXY ENV HTTP_PROXY="http://192.168.1.12:3128" --env HTTP_PROXY="http://192.168.1.12:3128"
    HTTPS_PROXY ENV HTTPS_PROXY="https://192.168.1.12:3128" --env HTTPS_PROXY="https://192.168.1.12:3128"
    FTP_PROXY ENV FTP_PROXY="ftp://192.168.1.12:3128" --env FTP_PROXY="ftp://192.168.1.12:3128"
    NO_PROXY ENV NO_PROXY="*.test.example.com,.example2.com" --env NO_PROXY="*.test.example.com,.example2.com"

    相关文章

      网友评论

        本文标题:配置docker使用 proxy

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