【问题标题】:Docker: npm install behind proxyDocker:代理后面的 npm 安装
【发布时间】:2023-03-03 23:23:02
【问题描述】:

我有这个 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 如果它以 ok npm info using npm@2.14.12 npm 结尾,它就可以工作 信息使用 node@v4.2.6 npm WARN package.json deployer-ui@1.0.0 否 描述 npm WARN package.json deployer-ui@1.0.0 没有存储库 场地。 npm WARN package.json deployer-ui@1.0.0 没有 README 数据 npm info 预安装 deployer-ui@1.0.0 npm info 尝试注册请求尝试 #1 在上午 7:09:23 npm http 请求 GET https://registry.npmjs.org/body-parser npm info 尝试注册 在上午 7:09:23 请求尝试 #1 npm http 请求 GET https://registry.npmjs.org/express npm info retry 会重试,error on 最后一次尝试:错误:无法建立隧道套接字, Cause=write EPROTO npm info retry 将重试,最后一次尝试出错: 错误:无法建立隧道套接字,原因=写入 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"

提前致谢。

【问题讨论】:

    标签: proxy docker npm


    【解决方案1】:

    首先,https_proxy 应该使用 http 网址,而不是 https 网址。

    其次,你不需要在你的 Dockfile 中嵌入你的代理设置:你可以使用build time variables

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

    最后,proxy settings at the docker service level 允许 docker 守护进程从 Internet 拉取图像。这并不意味着docker build 执行的unix 命令(RUN 指令)将从它们中受益。因此需要将它们作为构建时环境变量传递。

    【讨论】:

    • 我要注意构建变量也可以在 compose 文件中设置:docs.docker.com/compose/compose-file/#args
    • 为什么所有的 https_proxy 变量都使用 http url 而不是 https url?我一直看到这个,但不明白为什么
    • @Goku 无需在代理级别加密:serverfault.com/a/817684/783stackoverflow.com/a/47474561/6309
    • 是否可以将 docker 配置为在进行 docker build 时始终使用 http 代理?如果有很多预先构建的脚本或二进制文件正在执行 docker build,这将很有用。
    • @Goku 我不这么认为,除了创建别名。
    【解决方案2】:

    我也有同样的问题,不想在我的图像中设置任何代理信息,因为我不想依赖我的公司环境。

    我的解决方案是使用在网关模式下运行的 cntlm。为此,我在我的 cntlm 配置文件中将标志 Gateway 设置为 yes 以下允许规则:

     Gateway         yes
     # Allow local
     Allow           127.0.0.1
     # Allow docker subnetwork
     Allow           172.17.0.0/16
    

    然后我可以通过获取 dokcer0 接口地址(使用ifconfigcommand 获取)来运行我的 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 开始,您可以简单地configure the docker client proxy

    因此你的~/.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。但无论我尝试使用 docker 作为 IP,它都不起作用。我使用的是 Windows 10。我有 10.0.75.0 作为子网地址,对于 ipconfig /all 我有 10.0.75.1。 NPM 不会安装任何东西。任何想法?顺便说一句:如果我插入代理信息 user:pw@proxy.de 它工作正常。
    【解决方案3】:

    (你知道,这个包是我自己写的)

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

    只要运行:

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

    【讨论】:

      【解决方案4】:

      Docker Documentation 中所述,将以下内容添加到~/.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"
         }
       }
      }

      【讨论】:

        【解决方案5】:

        将此添加到 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。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-23
        • 2015-01-03
        • 2017-04-28
        • 2018-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多