如何使用Jenkins在HTTP_PROXY后面创buildDockerfile?

构buildDocker镜像可以在桌面上正常工作。 安装Node.js NPM依赖性照常工作。 但是,如果使用托pipe在公司代理之后的持续集成服务器(如Jenkins),那么构buildDocker镜像将失败。

Node.js NPM依赖关系

在构buildNode.js pacakges时,如果在克隆GIT依赖项时无法连接到GIT,则命令npm install会失败。

e1ce5e8407d1: Already exists Status: Image is up to date for node:0.10.33 ---> e1ce5e8407d1 Step 1 : RUN mkdir -p /usr/src/app ---> Using cache ---> 965cad0c68b0 Step 2 : WORKDIR /usr/src/app ---> Using cache ---> 4c498f0c07e9 Step 3 : COPY package.json /usr/src/app/ ---> b0662a8275fb Removing intermediate container 5aca20551452 Step 4 : RUN npm install ---> Running in 7ccf9e5362af npm WARN package.json newww@2.0.0 No README data npm WARN package.json Dependency 'async-cache' exists in both dependencies and devDependencies, using 'async-cache@^0.1.5' from dependencies npm ERR! git clone https://github.com/npm/npm2es.git Cloning into bare repository '/root/.npm/_git-remotes/https-github-com-npm-npm2es-git-60a75edb'... npm ERR! git clone https://github.com/npm/npm2es.git fatal: unable to access 'https://github.com/npm/npm2es.git/': Failed to connect to github.com port 443: Connection timed out 

Java Maven,Ruby,去Docker图像与依赖

构buildJava,Ruby或Go容器时也会发生这种情况,其中依赖项位于公司代理服务器上的存储库服务器中。

知道你可以configurationHTTP_PROXY环境variables的Docker,如何正确configurationDocker在CI环境中正确构build映像?

注意:Docker 1.9 可能有助于解决这个问题:

  • “ 问题14634 ”: 生成器 – 构build时间parameter passing (例如, HTTP_PROXY
  • “ PR 15182 ”: 支持在构build上下文中传递构build时variables

用法(build议):

 docker build --build-arg http_proxy=http://my.proxy.url --build-arg foo=bar <<MARK FROM busybox RUN <command that need http_proxy> ARG --description="foo's description" foo USER $foo MARK 

Docker守护进程HTTP代理

很多关于为Docker的守护进程设置HTTP_PROXY环境variables的文档。 环境variables只有在运行容器时才可用,所以它不会帮助我们。

解决scheme在Dockerfile中

尽pipe在Dockerfile中设置环境variablesHTTP_ENVhttp_env可能会有所帮助,但这对我们的原因也没有帮助。

ENV http_proxy http://proxy.mycompany.com:80

之所以这样,是因为每个特定的服务只以不同的方式来尊重HTTP代理设置。 我可以解决的方法如下。

  • NPM:NPM需要使用CLI命令设置HTTP_PROXYvariables。
  • GIT:GIT也需要使用CLI命令设置HTTP_PROXYvariables。
  • MAVEN:MVN命令需要在〜/ .m2 / settings.xml的用户目录下将HTTP_PROXY设置为XML文件。 对于Docker,您可以将其添加到根目录的“/root/.m2/settings.xml”目录(不安全,仅开发)或Dockerfile用户的主目录中。

例如,使用Dockerfile运行应用程序,我可以使用下面的Dockerfile构build一个图像:

 FROM node:0.10.33 # Prepare RUN mkdir -p /usr/src/app WORKDIR /usr/src/app # Use the cache for dependencies COPY package.json /usr/src/app/ # If building behind an http_proxy, set them for git and npm RUN git config --global http.proxy http://qypprdproxy02.ie.company.net:80 && \ npm config set proxy http://qypprdproxy02.ie.company.net:80 && \ npm config set https-proxy http://qypprdproxy02.ie.company.net:80 # Install dependencies RUN npm install # Copy all the source COPY . /usr/src/app # Execute the dev steps COPY ./numbat-config.example.js /usr/src/app/numbat-config.js COPY ./.env.example /usr/src/app/.evn RUN touch /usr/src/app/config.admin.js 

请注意,我已经使用CLI命令configuration了GIT和NPM,以便在运行NPM安装命令之前明确采用代理设置。 这样,NPM和GIT依赖关系将分别自动检索和克隆。

使用此Dockerfile构build映像的结果按预期工作:

 [root@pppdc9prd6dq newww]# fig build ... ... Building npmregistryserver... ---> Using cache ---> 965cad0c68b0 Step 2 : WORKDIR /usr/src/app ---> Using cache ---> 4c498f0c07e9 Step 3 : COPY package.json /usr/src/app/ ---> ae8ff7861246 Removing intermediate container ba1d7b8c9963 Step 4 : RUN npm config set proxy http://qypprdproxy02.ie.company.net:80 && npm config set https-proxy http://qypprdproxy02.ie.company.net:80 && npm install ---> Running in aa6e05d9c7a4 npm WARN package.json newww@2.0.0 No README data npm WARN package.json Dependency 'async-cache' exists in both dependencies and devDependencies, using 'async-cache@^0.1.5' from dependencies npm WARN deprecated extend@1.1.3: Please update to the latest version. > v8flags@1.0.8 install /usr/src/app/node_modules/gulp/node_modules/v8flags > node fetch.js > hiredis@0.1.17 install /usr/src/app/node_modules/hiredis > node-gyp rebuild make: Entering directory '/usr/src/app/node_modules/hiredis/build' CC(target) Release/obj.target/hiredis/deps/hiredis/hiredis.o CC(target) Release/obj.target/hiredis/deps/hiredis/net.o CC(target) Release/obj.target/hiredis/deps/hiredis/sds.o CC(target) Release/obj.target/hiredis/deps/hiredis/async.o AR(target) Release/obj.target/deps/hiredis.a COPY Release/hiredis.a CXX(target) Release/obj.target/hiredis/src/hiredis.o CXX(target) Release/obj.target/hiredis/src/reader.o SOLINK_MODULE(target) Release/obj.target/hiredis.node SOLINK_MODULE(target) Release/obj.target/hiredis.node: Finished COPY Release/hiredis.node make: Leaving directory '/usr/src/app/node_modules/hiredis/build' npm WARN engine hawk@0.10.2: wanted: {"node":"0.8.x"} (current: {"node":"0.10.33","npm":"2.1.11"}) > pngcrush-bin@1.0.0 postinstall /usr/src/app/node_modules/imagemin-pngcrush/node_modules/pngcrush-bin > node lib/install.js fetch : https://raw.githubusercontent.com/imagemin/pngcrush-bin/v1.0.0/vendor/linux/pngcrush ✔ pre-build test passed successfully! > dtrace-provider@0.3.1 install /usr/src/app/node_modules/npm-typeahead/node_modules/restify/node_modules/dtrace-provider > scripts/install.js npm WARN engine cryptiles@0.1.3: wanted: {"node":"0.8.x"} (current: {"node":"0.10.33","npm":"2.1.11"}) npm WARN engine sntp@0.1.4: wanted: {"node":"0.8.x"} (current: {"node":"0.10.33","npm":"2.1.11"}) npm WARN engine boom@0.3.8: wanted: {"node":"0.8.x"} (current: {"node":"0.10.33","npm":"2.1.11"}) npm WARN engine hoek@0.7.6: wanted: {"node":"0.8.x"} (current: {"node":"0.10.33","npm":"2.1.11"}) npm WARN cannot run in wd newww@2.0.0 gulp build (wd=/usr/src/app) newww-metrics@1.0.0 node_modules/newww-metrics murmurhash@0.0.2 node_modules/murmurhash npm-humans@2.0.1 node_modules/npm-humans leven@1.0.1 node_modules/leven chunk@0.0.2 node_modules/chunk npm-expansions@1.14.0 node_modules/npm-expansions similarity@1.0.1 node_modules/similarity truncate@1.0.4 node_modules/truncate 

这可以正常工作,并且可以在http代理之后有一个CI / CD环境来重build基于这个Dockerfile的图像。

我们正在做 …

 ENV http_proxy http://9.9.9.9:9999 ENV https_proxy http://9.9.9.9:9999 

并在docker文件结束…

 ENV http_proxy "" ENV https_proxy "" 

这个,现在(直到docker介绍构build环境variables),允许代理variables被用于构build而不公开暴露他们

我意识到这已经解决了OP,但是想分享什么最终为我工作,因为这个网页在search结果高的时候,我正在寻找我的问题的答案。 作为参考,我不能让Docker使用代理来通过点安装。

我必须做

 docker build --no-cache --build-arg HTTP_PROXY=$http_proxy \ --build-arg HTTPS_PROXY=$http_proxy --build-arg NO_PROXY=$no_proxy \ --build-arg http_proxy=$http_proxy --build-arg https_proxy=$http_proxy \ --build-arg no_proxy=$no_proxy -t myContainer /path/to/Dockerfile/directory 

在我的bashrc中设置了http_proxy和no_proxy。 请注意,我也有

 ENV http_proxy http://proxy.mycompany.com:80 

在我的Dockerfile中设置,但是这不足以让pip安装任何东西。 希望这可以帮助别人。