Docker:在代理后安装npm

19

我有这个Dockerfile:

FROM node:argon

ENV http_proxy http://user:pass@proxy.company.priv:3128
ENV https_proxy https://user:pass@proxy.company.priv:3128

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 8080
CMD [ "npm", "start" ]

但是我在执行npm install时遇到了以下错误:

npm info it worked if it ends with ok npm info using npm@2.14.12 npm info using node@v4.2.6 npm WARN package.json deployer-ui@1.0.0 No description npm WARN package.json deployer-ui@1.0.0 No repository field. npm WARN package.json deployer-ui@1.0.0 No README data npm info preinstall deployer-ui@1.0.0 npm info attempt registry request try #1 at 7:09:23 AM npm http request GET https://registry.npmjs.org/body-parser npm info attempt registry request try #1 at 7:09:23 AM npm http request GET https://registry.npmjs.org/express npm info retry will retry, error on last attempt: Error: tunneling socket could not be established, cause=write EPROTO npm info retry will retry, error on last attempt: Error: tunneling socket could not be established, cause=write EPROTO

我猜这是代理的原因。我也尝试过将

RUN npm config set proxy http://user:pass@proxy.company.priv:3128
RUN npm config set https-proxy http://user:pass@proxy.company.priv:3128

但仍然收到相同的错误。

此外,在我的文件/etc/systemd/system/docker.service.d/http-proxy.conf中,我有以下内容:

Environment="HTTP_PROXY=http://user:pass@proxy.company.priv:3128"
Environment="HTTPS_PROXY=https://user:pass@proxy.company.priv:3128"

提前致谢。

5个回答

31

首先,https_proxy 应该使用一个 http url,而不是 https url。

第二,您不需要在 Dockfile 中嵌入代理设置:可以使用构建时间变量

docker build --build-arg HTTP_PROXY=http://user:pass@proxy.company.priv:3128 --build-arg HTTPS_PROXY=http://user:pass@proxy.company.priv:3128 .

最后,docker 服务级别的代理设置 允许 docker 守护程序从 Internet 拉取镜像。这并不意味着由 docker build 执行的 unix 命令(RUN 指令)会从中受益。因此需要将它们作为构建时间环境变量传递。


1
我想指出,在compose文件中也可以设置构建变量: https://docs.docker.com/compose/compose-file/#args - lindhe
为什么所有的https_proxy变量都使用http URL而不是https URL?我一直看到这个问题,但从未理解原因。 - Goku
1
@Goku 代理级别不需要加密:https://serverfault.com/a/817684/783 和 https://dev59.com/T2kv5IYBdhLWcg3wqSj1#47474561 - VonC
在进行 Docker 构建时,是否可以配置 Docker 始终使用 HTTP 代理?如果有许多预构建的脚本或二进制文件正在进行 Docker 构建,则这将非常有用。 - Goku
@Goku 我不这么认为,除了创建一个别名。 - VonC

6
我也遇到了同样的问题,不想在我的镜像中设置任何代理信息,因为我不想依赖于我的公司环境。我的解决方案是使用运行在网关模式下的 cntlm。为此,我在我的 cntlm 配置文件中设置了以下允许规则:Gateway 标志设置为 yes
 Gateway         yes
 # Allow local
 Allow           127.0.0.1
 # Allow docker subnetwork
 Allow           172.17.0.0/16

然后,我通过获取ifconfig命令得到的dokcer0接口地址来运行我的Docker文件:

docker build -t my-image --build-arg HTTP_PROXY=http://172.17.0.1:3128 --build-arg HTTPS_PROXY=http://172.17.0.1:3128 .

docker run相同:

docker run --rm -e HTTP_PROXY=http://172.17.0.1:3128 --build-arg HTTPS_PROXY=http://172.17.0.1:3128 my-image

然而需要注意的是,自Docker 17.07版本开始,您可以简单地配置Docker客户端代理

因此,您的~/.docker/config.json应该如下:

{
  "proxies": {
      "default":{
          "httpProxy": "http://172.17.0.1:3128/",
          "httpsProxy": "http://172.17.0.1:3128/",
          "noProxy": "127.0.0.1,172.17.0.0/16,*.some.compagny.domain"
      }
}

我有同样的问题,但你的解决方案对我不起作用。我已经成功地在Firefox中运行了cntlm,并将代理设置为127.0.0.1:3128。但是,无论我尝试什么IP地址与docker一起使用,它都不起作用。我正在使用Windows 10。我有10.0.75.0作为子网地址,对于ipconfig /all,我有10.0.75.1。NPM将不会安装任何东西。有什么想法吗?顺便说一下:如果我插入代理信息user:pw@proxy.de,它可以正常工作。 - Matthis Kohli

3

我在Dockerfile中添加以下内容解决了问题:

RUN npm config set https-proxy http://user:password@proxy.company.priv:80
RUN npm config set proxy http://user:password@proxy.company.priv:80

我还需要添加 RUN npm config set registry http://registry.npmjs.org/ 来强制使用 HTTP 连接。 - Drarig29

0

Docker文档所述,将以下内容添加到~/.docker/config.json对我很有帮助:

{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://127.0.0.1:3001",
     "httpsProxy": "http://127.0.0.1:3001",
     "noProxy": "*.test.example.com,.example2.com"
   }
 }
}


0

仅供您知道,这个包是我自己编写的。

您可以使用docker-container-proxy,它允许为任何Docker容器配置代理,而无需编辑任何代码。

只需运行:

npx dockerproxy start --address company-proxy-address.com --port 8080
# Do anything else that needs a Proxy

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