簡體   English   中英

為 node.js 應用程序構建 docker 鏡像在代理之后失敗

[英]Building a docker image for a node.js app fails behind proxy

我在 docker 構建期間遇到 npm 問題。 我是一家公司代理人,閱讀了大約 30 篇解決類似問題的文章(和 stackoverflow 帖子)。 但是我仍然無法克服這一點。

我能夠“npm install”項目並在 docker build 過程之外獲取所有必要的依賴項(但也使用代理),但不是在 docker 構建過程中。

到目前為止我嘗試了什么:

  1. 直接使用代理(連同硬編碼的身份驗證數據)以及通過 CNTLM。 下面的描述是使用 CNTLM 時的情況。
  2. 使用如下所示的 http 存儲庫和 strict_ssl false。

npm config set strict-ssl=false \

npm config set registry=http://registry.npmjs.org/ \

  1. 將代理設置作為 --build-arg、env 並通過 RUN 參數傳遞

  2. 從干凈的 git checkout(沒有 node_modules)開始並在運行 npm install 之后

我正在嘗試構建:

$ sudo docker build --build-arg HTTP_PROXY=http://127.0.0.1:3128 --build-arg HTTPS_PROXY=http://127.0.0.1:3128 .

輸出

Sending build context to Docker daemon 226.6 MB
Step 1 : FROM node:argon
 ---> c74c117ed521
Step 2 : ENV http_proxy http://127.0.0.1:3128/
 ---> Using cache
 ---> ad2e2df7429b
Step 3 : ENV https_proxy http://127.0.0.1:3128/
 ---> Using cache
 ---> 75fb2eb0bb22
Step 4 : RUN mkdir -p /usr/src/app
 ---> Using cache
 ---> ee79de37d6d7
Step 5 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 404356f5def0
Step 6 : COPY package.json /usr/src/app/
 ---> Using cache
 ---> a2ec47267628
Step 7 : RUN git config --global http.proxy http://127.0.0.1:3128/
 ---> Running in 3cd5db8b1371
 ---> 7353cd94b67a
Removing intermediate container 3cd5db8b1371
Step 8 : RUN npm install
 ---> Running in 79ed0eb809d8
npm info it worked if it ends with ok
npm info using npm@2.15.5
npm info using node@v4.4.6
npm info preinstall app
npm info attempt registry request try #1 at 10:24:02 AM
npm http request GET https://registry.npmjs.org/bufferutil
npm info attempt registry request try #1 at 10:24:02 AM
npm http request GET https://registry.npmjs.org/connect-mongo
<snip>

npm info retry will retry, error on last attempt: Error: tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:3128
npm info retry will retry, error on last attempt: Error: tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:3128
<snip>

npm ERR! Linux 3.13.0-88-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v4.4.6
npm ERR! npm  v2.15.5
npm ERR! code ECONNRESET

npm ERR! network tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:3128
npm ERR! network This is most likely not a problem with npm itself
npm ERR! network and is related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network 
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

npm ERR! Please include the following file with any support request:
npm ERR!     /usr/src/app/npm-debug.log

這是我的泊塢窗腳本

FROM node:argon

ENV http_proxy http://127.0.0.1:3128/
ENV https_proxy http://127.0.0.1:3128/

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/

# setup proxies
RUN git config --global http.proxy http://127.0.0.1:3128/ && \
    npm config set strict-ssl=false \
    npm config set registry=http://registry.npmjs.org/ \
    npm config set proxy=http://127.0.0.1:3128/ && \
    npm config set https-proxy=http://127.0.0.1:3128/

# Install dependencies for node.js
RUN npm install

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

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

使用--build-arg的方法是正確的方法:您只希望在構建Docker映像時使用代理設置,並且不將其包含在Dockerfile中,因此它與特定環境無關(您不需要上面的ENV條目)。

您的問題是您試圖在docker build localhost用作cntlm代理,這是無效的,因為在構建時,它將指向運行構建的docker容器,但實際上應指向提供cntlm的主機的地址在docker網絡中。

為了使它起作用,您可以將cntlm配置為偵聽多個接口,然后激活網關模式,以便可以從其他計算機上使用它。 這樣,在構建映像時,您會將請求從Docker實例發送到主機。

我的Docker橋網絡如下(我的主機以docker0 172.17.0.1地址獲取):

$ docker network inspect bridge
...
            "Config": [
            {
                "Subnet": "172.17.0.0/16",
                "Gateway": "172.17.0.1"
            }
...

在我的cntlm.conf

...
Listen          127.0.0.1:3128
Listen          172.17.0.1:3128
...
Gateway yes
Allow           127.0.0.1/32
Allow           172.17.0.0/16
Deny            0/0
...

使用此配置,cntlm將偵聽localhost和docker bridge網絡,僅允許來自任何docker容器的遠程連接。

然后在使用npm構建映像時使用代理設置:

$ docker build --build-arg=HTTP_PROXY=http://172.17.0.1:3128 --build-arg=HTTPS_PROXY=http://172.17.0.1:3128 .

我希望能有所幫助,我知道在企業網絡中實現所有這些確實是皮塔餅!

編輯2016年8月18日

我今天發現的事情是,如果您使用v2格式的docker-compose文件,則啟動compose文件將為您的容器創建一個新的網絡。 這意味着您需要相應地調整cntlm文件以接受來自這些新范圍的連接。

例如,我的一個撰寫文件剛剛在172.19.0.0/16下創建了一個網絡,但是我的cntlm配置僅允許來自172.17.0.0/16連接。 如果遇到連接問題,請檢查系統日志以識別問題。

https://docs.docker.com/compose/networking/

有完全一樣的情況! 像這樣在.npmrc中設置proxyhttp-proxy對我有用。

proxy = http://your-company-proxy
https-proxy = http://your-company-proxy

如果你跑步也可能有效

RUN npm config set proxy http://your-company-proxy
RUN npm config set https-proxy http://your-company-proxy

在你的 Dockerfile 中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM