Dockerfile中使用代理的Pip

18

我正试图为elasticsearch-curator构建一个Docker镜像,

以下是dockerfile:

FROM alpine:3.7

RUN adduser -S curator

RUN apk add --update \
    python \
    python-dev \
    py-pip \
    build-base \
  && pip install virtualenv \
  && pip install elasticsearch-curator \
  && rm -rf /var/cache/apk/*

USER curator

ENTRYPOINT [ "/usr/bin/curator"]

我的问题是我在代理下工作,因此必须使用以下命令构建我的镜像:

docker build  --no-cache --build-arg HTTP_PROXY=http://xx.xx.xx.xx:xx -t elasticsearch-curator:5.4 .

但是当它想要获取 virtualenv 时,我得到了以下信息:
Collecting virtualenv
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fb8259ed350>, 'Connection to pypi.python.org timed out. (connect timeout=15)')': /simple/virtualenv/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fb8259ed210>, 'Connection to pypi.python.org timed out. (connect timeout=15)')': /simple/virtualenv/

我发现有人解决了插入问题。
ENV http_proxy http://proxy-chain.xxx.com:911/
ENV https_proxy http://proxy-chain.xxx.com:912/

在Dockerfile中设置http_proxy是不可能的,因为我的代理只在我建立映像时有效。如果其他人想要构建该映像,他将需要从Dockerfile中删除http_proxy环境变量。有没有其他方法可以实现这一目标?这似乎是一个非常常见的用例...

“--build-arg” 解决方案 应该 起作用了。 你的主机是 Windows 还是 Linux?在代理中设置了一些用户/密码吗?如果是这种情况,那么 ard 应该是 http_proxy=http://USERNAME:PASSWORD@my-proxy.com:3128/ - Alfonso Tienda
不,我不使用登录/密码。构建参数可以用于下载alpine镜像,但对于pip无效 :( - Juliatzin
4个回答

31

我通过在命令行中添加HTTPS_PROXY解决了这个问题:

docker build  --no-cache --build-arg HTTP_PROXY=http://xx.xx.xx.xx:xx --build-arg HTTPS_PROXY=http://xx.xx.xx.xx:xx -t elasticsearch-curator:5.4 .

17
不要在Dockerfile中包含代理设置。
如果您已经在主机上正确配置了代理设置,可以使用--network=host构建docker镜像。这将使构建命令使用主机的网络设置。
docker build  --no-cache --network=host -t elasticsearch-curator:5.4 .

这对我来说解决了问题。如果没有--network=host,我无法使用pip安装flask。 - Mike T
适用于“FROM python:3.7”。 - rjdkolb

1
我认为这是因为pip install需要明确的代理参数。
尝试创建一个install.sh以进行pip安装。
如果配置了代理(作为build-arg传递,即在环境中设置),请使用以下命令进行安装:
pip install --proxy=https://user@mydomain:port virtualenv

With env variable:

pip install --proxy=$HTTP_PROXY virtualenv

如果没有代理,请使用pip install。

我没有测试过它,但我在命令行中找到了解决方案。即使它能够工作,我也不想在我的Dockerfile中编写代理变量,我更喜欢纯命令行。无论如何,感谢您的帮助! - Juliatzin
我明白了...这只是为了通过https代理 :-P。 - Alfonso Tienda

1
你可以通过位于主目录或用户目录下的~\.docker\config.json文件来配置所有客户端代理。
{ 
  "credsStore": "wincred",
  "auths": {},
  "stackOrchestrator": "swarm",
  "proxies":
  {
   "default":
   {
    "httpProxy": "http://127.0.0.1:3001",
    "noProxy": "*.test.example.com,.example2.com"
   }
  }
}

前三个条目是默认存在的,只需在文件中添加"proxies"部分即可。
来源:https://docs.docker.com/network/proxy/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接