Docker 守护进程 HTTP 代理
有很多关于为 Docker 守护进程设置 HTTP_PROXY 环境变量的文档。环境变量仅在运行容器时可用,因此在这里对我们没有帮助。
Dockerfile 中的解决方案
尽管在 Dockerfile 中设置环境变量HTTP_ENV或http_env可能会有所帮助,但它也无助于我们的事业。
ENV http_proxy http://proxy.mycompany.com:80
原因是每个特定服务仅以不同的方式支持 HTTP 代理设置。我可以解决的方法如下。
- NPM:NPM 需要使用 CLI 命令设置 HTTP_PROXY 变量。
- GIT:GIT 还需要使用 CLI 命令设置 HTTP_PROXY 变量。
- MAVEN:MVN 命令需要在~/.m2/settings.xml的用户目录下将 HTTP_PROXY 设置为 XML 文件。对于 Docker,您可以将其添加到根目录的“/root/.m2/settings.xml”目录(不安全,仅限开发)或 Dockerfile 的用户主目录。
例如,使用 Dockerfile 运行应用程序,我可以使用以下 Dockerfile 构建映像:
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 命令配置了 GIT 和 NPM,以在运行 NPM 安装命令之前显式获取代理设置。这样,NPM 和 GIT 依赖项将分别被自动检索和克隆。
使用此 Dockerfile 构建映像的结果按预期工作:
[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 环境,以基于此 Dockerfile 重建图像。