Skip to content

Instantly share code, notes, and snippets.

@metathrone
Last active February 9, 2023 06:24
Show Gist options
  • Save metathrone/c86f38a7b7ac196265b1887bc951217b to your computer and use it in GitHub Desktop.
Save metathrone/c86f38a7b7ac196265b1887bc951217b to your computer and use it in GitHub Desktop.

docker网络代理配置

dockerd代理

在执行docker pull时,是由守护进程dockerd来执行

linux

因此,代理需要配在dockerd的环境中。而dockerd是受systemd所管控,因此实际是systemd的配置

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

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

[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7890"
Environment="HTTPS_PROXY=http://127.0.0.1:7890"
Environment="NO_PROXY=localhost,127.0.0.1,.example.com"

Dokcer Desktop for Windows

打开右上角齿轮打开设置-Resources-Proxies

配置http代理与https代理为 http://127.0.0.1:7890

Container代理

用户配置设置

在容器运行阶段,如果需要代理上网,则需要配置以下配置,只在Docker 17.07及以上版本生效

linux

路径 ~/.docker/config.json

{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://127.0.0.1:7890",
     "httpsProxy": "http://127.0.0.1:7890",
     "noProxy": "localhost,127.0.0.1,.example.com"
   }
 }
}

windows

路径为 %UserProfile%\.docker

Dokcer Desktop for Windows 维护了wsl2 backend对宿主机的地址映射,可以使用 host.docker.internal 作为代理地址

wsl2与windows下需要分别进行设置

{
  "credsStore": "desktop",
  "proxies":
 {
   "default":
   {
     "httpProxy": "http://host.docker.internal:7890",
     "httpsProxy": "http://host.docker.internal:7890",
     "noProxy": "localhost,127.0.0.1,.example.com"
   }
 }
}

config.json为用户级的配置,除了proxies,docker login等相关信息也会在其中。而且还可以配置信息展示的格式、插件参数等。

环境变量配置

从环境变量传入代理参数设置,可以在启动时传入变量、构建时传入变量或在启动后在容器的shell里配置变量

启动时传入

docker run -d \
    -e HTTP_PROXY=http://127.0.0.1:7890 \
    -e HTTPS_PROXY=http://127.0.0.1:7890 \
    -e NO_PROXY=localhost,127.0.0.1,.example.com \
    your/image:tag

构建时传入

FROM local/c7-systemd

ENV MY_PROXY_URL="http://127.0.0.1:7890"
ENV HTTP_PROXY=$MY_PROXY_URL \
    HTTPS_PROXY=$MY_PROXY_URL \
    FTP_PROXY=$MY_PROXY_URL \
    http_proxy=$MY_PROXY_URL \
    https_proxy=$MY_PROXY_URL \
    ftp_proxy=$MY_PROXY_URL


RUN yum -y install httpd; yum clean all; systemctl enable httpd.service

EXPOSE 80

CMD ["/usr/sbin/init"]

启动后在容器的shell里配置

MY_PROXY_URL="http://proxy.esl.xxx.com:80"

HTTP_PROXY=$MY_PROXY_URL \
    HTTPS_PROXY=$MY_PROXY_URL \
    FTP_PROXY=$MY_PROXY_URL \
    http_proxy=$MY_PROXY_URL \
    https_proxy=$MY_PROXY_URL \
    ftp_proxy=$MY_PROXY_URL

export http_proxy
export https_proxy

代理域名解析

如果代理是通过域名连接时,需要传入dns配置,有两种方式进行解决

启动时传入dns

docker run -d \
    -e HTTP_PROXY=http://proxy.example.com:7890 \
    -e HTTPS_PROXY=http://proxy.example.com:7890 \
    -e NO_PROXY=localhost,127.0.0.1,.example.com \
    --dns=119.29.29.29 \
    your/image:tag

修改 Docker daemon配置

在每个 container 运行前,会继承 Docker daemon 的配置,在 /etc/docker/daemon.json 文件下.

{
  "dns" : [
    "8.8.4.4",
    "8.8.8.8",
    "Your_DNS_SERVER"
  ],

  "registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]
}

docker build代理

虽然docker build的本质,也是启动一个容器,但是环境会略有不同,用户级配置无效。在构建时,需要注入http_proxy等参数。

docker build . \
    --build-arg "HTTP_PROXY=http://127.0.0.1:7890" \
    --build-arg "HTTPS_PROXY=http://127.0.0.1:7890" \
    --build-arg "NO_PROXY=localhost,127.0.0.1,.example.com" \
    -t your/image:tag

注意:无论是docker run还是docker build,默认是网络隔绝的。

如果代理使用的是localhost:3128这类,则会无效。

这类仅限本地的代理,必须加上--network host才能正常使用。

而一般则需要配置代理的外部IP,而且代理本身要开启gateway模式。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment