我在我的 Ubuntu 13.10 (Saucy Salamander) 上安装了 Docker,当我在控制台中输入时:
sudo docker pull busybox
我收到以下错误:
Pulling repository busybox
2014/04/16 09:37:07 Get https://index.docker.io/v1/repositories/busybox/images: dial tcp: lookup index.docker.io on 127.0.1.1:53: no answer from server
码头工人版本:
$ sudo docker version
Client version: 0.10.0
Client API version: 1.10
Go version (client): go1.2.1
Git commit (client): dc9c28f
Server version: 0.10.0
Server API version: 1.10
Git commit (server): dc9c28f
Go version (server): go1.2.1
Last stable version: 0.10.0
我在没有身份验证的代理服务器后面,这是我的 /etc/apt/apt.conf
文件:
Acquire::http::proxy "http://192.168.1.1:3128/";
Acquire::https::proxy "https://192.168.1.1:3128/";
Acquire::ftp::proxy "ftp://192.168.1.1:3128/";
Acquire::socks::proxy "socks://192.168.1.1:3128/";
我究竟做错了什么?
Acquire::socks::proxy
表示为所有以 socks
方案开头的 URL 设置代理。由于您的 sources.list
没有任何 socks://
URL,因此该行将被完全忽略。
docker-compose
呢?
这是代理 HTTP 的官方 Docker 文档的链接:https://docs.docker.com/config/daemon/systemd/#httphttps-proxy
快速概述:
首先,为 Docker 服务创建一个 systemd 插入目录:
mkdir /etc/systemd/system/docker.service.d
现在创建一个名为 /etc/systemd/system/docker.service.d/http-proxy.conf
的文件,添加 HTTP_PROXY
和 HTTPS_PROXY
环境变量:
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80/"
Environment="HTTPS_PROXY=http://proxy.example.com:80/"
如果您有需要在不使用代理的情况下联系的内部 Docker 注册表,您可以通过 NO_PROXY
环境变量指定它们:
Environment="HTTP_PROXY=http://proxy.example.com:80/"
Environment="HTTPS_PROXY=http://proxy.example.com:80/"
Environment="NO_PROXY=localhost,127.0.0.0/8,docker-registry.somecorporation.com"
冲洗变化:
$ sudo systemctl daemon-reload
验证是否已加载配置:
$ sudo systemctl show --property Environment docker
Environment=HTTP_PROXY=http://proxy.example.com:80/
Environment=HTTPS_PROXY=http://proxy.example.com:80/
重启 Docker:
$ sudo systemctl restart docker
关于 HTTP_PROXY
与 HTTPS_PROXY
的脚注:很长一段时间以来,单独设置 HTTP_PROXY
就足够了。但是在 20.10.8 版本中,Docker 已经转移到 Go 1.16,它改变了这个变量的语义:https://golang.org/doc/go1.16#net/http
对于 https://
URL,代理现在由 HTTPS_PROXY
变量确定,没有后备在 HTTP_PROXY
。
您的 APT 代理设置与 Docker 无关。
Docker 使用 HTTP_PROXY 环境变量(如果存在)。例如:
sudo HTTP_PROXY=http://192.168.1.1:3128/ docker pull busybox
但是,我建议您查看一下您的/etc/default/docker
配置文件:您应该有一行可以取消注释(并且可能进行调整)以自动应用您的代理设置。然后重启 Docker 服务器:
service docker restart
在 CentOS 上,Docker 的配置文件位于:
/etc/sysconfig/docker
添加以下行帮助我让 Docker 守护程序在代理服务器后面工作:
HTTP_PROXY="http://<proxy_host>:<proxy_port>"
HTTPS_PROXY="http://<proxy_host>:<proxy_port>"
export
的情况下才有效。你确定这是正确的吗?
export
仅适用于 CentOS6,不支持 CentOS7
export HTTP_PROXY=...
是 Bashism,对于非 Bash shell(例如 /bin/sh 可能是什么),使用两行 HTTP_PROXY=...
然后 export HTTP_PROXY
也就是说,我根本不需要 export
。
如果您使用新的 Docker for Mac(或 Docker for Windows),只需右键单击 Docker 托盘图标并选择首选项(Windows:设置),然后转到高级,然后在代理下指定您的代理设置。单击应用并重新启动并等待 Docker 重新启动。
在 Ubuntu 上,您需要为 Docker 守护进程设置 http_proxy,而不是客户端进程。这是在 /etc/default/docker
中完成的(参见 here)。
要扩展 Arun's answer,使其在 CentOS 7 中工作,我必须删除“导出”命令。所以编辑
/etc/sysconfig/docker
并添加:
HTTP_PROXY="http://<proxy_host>:<proxy_port>"
HTTPS_PROXY="https://<proxy_host>:<proxy_port>"
http_proxy="${HTTP_PROXY}"
https_proxy="${HTTPS_PROXY}"
然后重启 Docker:
sudo service docker restart
为什么本地绑定代理不起作用
问题
如果您正在运行 本地绑定 代理,例如在 127.0.0.1:8989
上侦听,则它在 Docker for Mac 中不起作用。从 Docker documentation:
我想从容器连接到主机上的服务 Mac 的 IP 地址不断变化(如果您没有网络访问权限,则没有)。我们目前的建议是在 Mac 的 lo0 接口上附加一个未使用的 IP;例如:sudo ifconfig lo0 alias 10.200.10.1/24,并确保您的服务正在侦听此地址或 0.0.0.0(即不是 127.0.0.1)。然后容器可以连接到这个地址。
Docker 服务器端也是如此。 (要了解 Docker 的服务器端和客户端,请尝试运行 docker version
。)服务器端运行在具有自己的 localhost
的虚拟化层上。因此,它不会连接到主机操作系统 localhost
上的代理服务器。
解决方案
因此,如果您使用像我这样的本地绑定代理,基本上您必须执行以下操作才能使其与 Docker for Mac 一起使用:
让您的代理服务器监听 0.0.0.0 而不是 127.0.0.1。注意:您需要适当的防火墙配置以防止对其进行恶意访问。向 lo0 接口添加一个环回别名,例如 10.200.10.1/24: sudo ifconfig lo0 alias 10.200.10.1/24 在 Docker 托盘菜单的 Preferences 中将 HTTP 和/或 HTTPS 代理设置为 10.200.10.1:8989(假设代理服务器正在监听 8989 端口)。
之后,通过在未下载的图像的新容器中运行命令来测试代理设置:
$ docker rmi -f hello-world
...
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for hello-world:latest
...
注意:由 ifconfig
设置的环回别名在重启后不会保留。使其持久化是另一个主题。请用日语检查 this blog post(谷歌翻译可能会有所帮助)。
这是对我有用的修复:Ubuntu,Docker 版本:1.6.2
在文件 /etc/default/docker
中,添加以下行:
export http_proxy='http://<host>:<port>'
重启 Docker
sudo service docker restart
要将 Docker 配置为使用代理,您需要将 HTTPS_PROXY / HTTP_PROXY 环境变量添加到 Docker sysconfig 文件 (/etc/sysconfig/docker
)。
根据您使用的是 init.d
还是服务工具,您需要添加“export”语句(由于 Debian Bug report logs - #767441. Examples in /etc/default/docker are misleading regarding the supported syntax):
HTTPS_PROXY="https://<user>:<password>@<proxy-host>:<proxy-port>"
HTTP_PROXY="https://<user>:<password>@<proxy-host>:<proxy-port>"
export HTTP_PROXY="https://<user>:<password>@<proxy-host>:<proxy-port>"
export HTTPS_PROXY="https://<user>:<password>@<proxy-host>:<proxy-port>"
Docker 存储库(Docker Hub)仅支持 HTTPS。要让 Docker 使用 SSL 拦截代理,您必须将代理根证书添加到系统信任库。
对于 CentOS,将文件复制到 /etc/pki/ca-trust/source/anchors/
并更新 CA 信任库并重新启动 Docker 服务。
如果您的代理使用 NTLMv2 身份验证 - 您需要使用 Cntlm 等中间代理来桥接身份验证。 This blog post explains it in detail。
在新版本的 Docker docker-engine 中,在基于 systemd 的发行版中,您应该将环境变量行添加到 /lib/systemd/system/docker.service,正如其他人所提到的:
Environment="HTTP_PROXY=http://hostname_or_ip:port/"
安装 Docker 后,执行以下操作:
[mdesales@pppdc9prd1vq ~]$ sudo HTTP_PROXY=http://proxy02.ie.xyz.net:80 ./docker -d &
[2] 20880
然后,您可以拉动或做任何事情:
mdesales@pppdc9prd1vq ~]$ sudo docker pull base
2014/04/11 00:46:02 POST /v1.10/images/create?fromImage=base&tag=
[/var/lib/docker|aa088847] +job pull(base, )
Pulling repository base
b750fe79269d: Download complete
27cf78414709: Download complete
[/var/lib/docker|aa088847] -job pull(base, ) = OK (0)
因为我还不能发表评论:
对于 CentOS 7,我需要在“docker.service”中激活 EnvironmentFile,如下所述:Control and configure Docker with systemd。
编辑:我正在添加我的解决方案,如 Nilesh 所述。我需要打开“/etc/systemd/system/docker.service”,我必须在该部分中添加
[服务] EnvironmentFile=-/etc/sysconfig/docker
只有这样文件“etc/sysconfig/docker”才加载到我的系统上。
如果使用 socks5 代理,这是我在 Docker 17.03.1-ce 上设置“all_proxy”的测试,它有效:
# Set up socks5 proxy server
ssh sshUser@proxyServer -C -N -g -D \
proxyServerIp:9999 \
-o ExitOnForwardFailure=yes \
-o ServerAliveInterval=60
# Configure dockerd and restart.
# NOTICE: using "all_proxy"
mkdir -p /etc/systemd/system/docker.service.d
cat > /etc/systemd/system/docker.service.d/http-proxy.conf <<EOF
[Service]
Environment="all_proxy=socks5://proxyServerIp:9999"
Environment="NO_PROXY=localhost,127.0.0.1,private.docker.registry.com"
EOF
systemctl daemon-reload
systemctl restart docker
# Test whether can pull images
docker run -it --rm alpine:3.5
为了解决 Docker 构建中 curl 的问题,我在 Dockerfile 中添加了以下内容:
ENV http_proxy=http://infoprx2:8080
ENV https_proxy=http://infoprx2:8080
RUN apt-get update && apt-get install -y curl vim
请注意,ENV 语句位于 RUN 语句之前。
为了使 Docker 守护进程能够访问 Internet(我使用带有 boot2docker 的 Kitematic),我在 /var/lib/boot2docker/profile
中添加了以下内容:
export HTTP_PROXY=http://infoprx2:8080
export HTTPS_PROXY=http://infoprx2:8080
然后我用 sudo /etc/init.d/docker restart
重新启动了 Docker。
Windows 的完整解决方案,用于配置代理设置。
< user>:< password>@< proxy-host>:< proxy-port>
您可以通过右键单击 Docker 图标中的设置,然后单击代理直接对其进行配置。
在那里您可以配置代理地址、端口、用户名和密码。
以这种格式:
< user>:< password>@< proxy-host>:< proxy-port>
例子:
"geronimous:mypassword@192.168.44.55:8080"
仅此而已。
如果你在 Ubuntu 上,你应该执行这个命令:
export https_proxy=http://your_name:password@ip_proxy:port docker
并重新加载 Docker:
service docker.io restart
或者使用 nano 前往 /etc/docker.io
...
如果您在 Ubuntu 中,请执行这些命令来添加您的代理。
sudo nano /etc/default/docker
并取消注释指定的行
#export http_proxy = http://username:password@10.0.1.150:8050
并将其替换为您适当的代理服务器和用户名。
然后使用以下命令重新启动 Docker:
service docker restart
现在您可以在代理后面运行 Docker 命令:
docker search ubuntu
也许您需要设置小写变量。就我而言,我的 /etc/systemd/system/docker.service.d/http-proxy.conf 文件如下所示:
[Service]
Environment="ftp_proxy=http://<user>:<password>@<proxy_ip>:<proxy_port>/"
Environment="http_proxy=http://<user>:<password>@<proxy_ip>:<proxy_port>/"
Environment="https_proxy=http://<user>:<password>@<proxy_ip>:<proxy_port>/"
祝你好运! :)
我在防火墙后面也面临同样的问题。请按照以下步骤操作:
$ sudo vim /etc/systemd/system/docker.service.d/http_proxy.conf
[Service]
Environment="HTTP_PROXY=http://username:password@IP:port/"
不要使用或删除 https_prxoy.conf 文件。
重新加载并重新启动 Docker 容器:
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557*********************************8
Status: Downloaded newer image for hello-world:latest
在 1.0.1 版中,简单地设置代理环境变量对我没有帮助...我必须使用“http_proxy”变量的正确值更新 /etc/default/docker.io
文件。
在带有 Docker 1.9.1 的 Ubuntu 14.04 (Trusty Tahr) 上,我只是取消了 http_proxy
行的注释,更新了值,然后重新启动了 Docker 服务。
export http_proxy="http://proxy.server.com:80"
接着
service docker restart
从环境变量中删除代理
unset http_proxy
unset https_proxy
unset no_proxy
然后重新启动你的码头工人
在 RHEL6.6 上仅此方法有效(注意使用 export
):
/etc/sysconfig/docker
export http_proxy="http://myproxy.example.com:8080"
export https_proxy="http://myproxy.example.com:8080"
注意:两者都可以使用 http
协议。)
感谢https://crondev.com/running-docker-behind-proxy/
在我的网络中,Ubuntu 在企业 ISA 代理服务器后面工作。它需要身份验证。我尝试了上面提到的所有解决方案,但没有任何帮助。真正有帮助的是在没有域名的文件 /etc/systemd/system/docker.service.d/https-proxy.conf
中编写代理行。
代替
Environment="HTTP_PROXY=http://user@domain:password@proxy:8080"
或者
Environment="HTTP_PROXY=http://domain\user:password@proxy:8080"
以及我尝试使用的其他一些替代品,例如 @ -> %40
或 \ -> \\
Environment="HTTP_PROXY=http://user:password@proxy:8080"
它现在有效。
尝试这个:
sudo HTTP_PROXY=http://<IP address of proxy server:port> docker -d &
这并不能完全回答问题,但可能会有所帮助,特别是如果您不想处理服务文件。
如果您是托管图像的人,一种方法是将图像转换为 tar 存档,而不是在服务器上使用以下内容。
docker save <image-name> --output <archive-name>.tar
只需下载存档并将其转回图像即可。
docker load <archive-name>.tar
已按照以下步骤解决了该问题:
第 1 步:sudo systemctl start docker
第 2 步:sudo systemctl enable docker
(创建从 /etc/systemd/system/multi-user.target.wants/docker.service
到 /usr/lib/systemd/system/docker.service
的符号链接。)
第 3 步:sudo systemctl status docker
第 4 步:sudo mkdir -p /etc/systemd/system/docker.service.d
第 5 步:sudo vi /etc/systemd/system/docker.service.d/proxy.conf
如下设置代理
[Service]
Environment="HTTP_PROXY=http://proxy.server.com:80"
Environment="HTTPS_PROXY=http://proxy.server.com:80"
Environment="NO_PROXY=.proxy.server.com,*.proxy.server.com,localhost,127.0.0.1,::1"
第 6 步:sudo systemctl daemon-reload
第 7 步:sudo systemctl restart docker.service
第 8 步:vi /etc/environment and source /etc/environment
http_proxy=http://proxy.server.com:80
https_proxy=http://proxy.server.com:80
ftp_proxy=http://proxy.server.com:80
no_proxy=127.0.0.1,10.0.0.0/8,3.0.0.0/8,localhost,*.abc.com
我遇到了一个问题,比如我需要使用代理来使用谷歌的 dns 来实现项目的依赖以及同时与私有服务器通信所需的 API 请求。
对于 RHEL7,我这样配置系统:
转到目录 /etc/sysconfig/docker
Environment=http_proxy="http://ip:port"
Environment=https_proxy="http://ip:port"
Environment=no_proxy="hostname"
然后保存文件并使用命令:
sudo systemctl 重启 docker
之后配置你的 Dockerfile :首先设置环境结构:
ENV http_proxy http://ip:port
ENV https_proxy http://ip:port
ENV no_proxy "hostname"
就这样! :)
不定期副业成功案例分享
/etc/default/docker
不起作用。也许我应该像为 Centos 记录的那样删除export
。sudo systemctl restart docker
是它工作的严格要求。systemctl
不适用于 ubuntu 14.04,它使用upstart
来启动服务。