Docker: npm install behind proxy

2019-03-19 17:57发布

问题:

I have this 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" ]

But I get this error, in npm install step:

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

I guess it is due to the proxy. I have also tried to put

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

but still getting the same error.

Moreover, in my file /etc/systemd/system/docker.service.d/http-proxy.conf I have this:

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

Thanks in advance.

回答1:

First the https_proxy should use an http url, not an https url.

Second, you don't need to embed your proxy settings in your Dockfile: you can use 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 .

Finally, proxy settings at the docker service level allows the docker daemon to pull images from internet. It does not mean the unix command executed (RUN directive) by docker build would benefit from them. Hence the need to pass them as build-time environment variables.



回答2:

I also had the same issue and did not want to set any proxy information in my image as I did not want be dependant of my company environment.

My solution was to use a cntlm running in gateway mode. To do so I put the flag Gateway set to yes the following allow rules in my cntlm configuration file:

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

Then I was able to run my docker file by getting the dokcer0 interface address (got with ifconfigcommand):

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 .


回答3:

(Just that you know, this package was written by myself)

You can use docker-container-proxy, it allows configuration of a proxy for any docker container without editing any code.

Just run:

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


标签: proxy docker npm