dockerbuild设+私人NPM(+私人docker枢纽)
我有一个在Docker容器中运行的应用程序。 它需要来自公司的私人NPM注册处(Sinopia)的一些私人模块,并且访问这些需要用户authentication。 Dockerfile是FROM iojs:latest
。
我努力了:
1)在项目根目录下创build.npmrc文件,这实际上没有什么区别,npm似乎忽略了它。2)使用envvariables为NPM_CONFIG_REGISTRY
, NPM_CONFIG_USER
等,但用户不login。
从本质上讲,我似乎没有办法在docker build
过程中对用户进行身份validation。 我希望有人可能已经遇到了这个问题(似乎是一个明显的问题),并有一个很好的解决方法。
(最重要的是,我在Docker Hub上使用自动构build(在推送时触发),以便我们的服务器可以使用预先构build的映像访问私人Dockerregistry。)
有没有好的方法:1)在构build时注入NPM的凭据(所以我不需要向我的Dockerfile提交凭据)或者2)以另一种方式,我还没有想到呢?
我发现在为node.js / io.js容器( you/iojs
)创build一个基本图像方面有些优雅的解决scheme:
- 用你想用于docker的用户login你的私有npmregistry
- 复制这个生成的
.npmrc
文件
示例.npmrc
:
registry=https://npm.mydomain.com/ username=dockerUser email=docker@mydomain.com strict-ssl=false always-auth=true //npm.mydomain.com/:_authToken="someAuthToken"
- 创build一个适当复制
.npmrc
文件的.npmrc
。
这里是我的Dockerfile
(基于iojs:onbuild
):
FROM iojs:2.2.1 MAINTAINER YourSelf # Exclude the NPM cache from the image VOLUME /root/.npm # Create the app directory RUN mkdir -p /usr/src/app WORKDIR /usr/src/app # Copy npm config COPY .npmrc /root/.npmrc # Install app ONBUILD COPY package.json /usr/src/app/ ONBUILD RUN npm install ONBUILD COPY . /usr/src/app # Run CMD [ "npm", "start" ]
- 让你所有的node.js / io.js容器
FROM you/iojs
,你很好去。
对于那些通过谷歌寻找这篇文章,并仍在寻找一种替代方式,不涉及离开你的泊坞窗图像和容器上的私人npm令牌:
我们可以通过在.npmrc
docker build
之前进行npm install
来获得这个工作(通过这样做,可以让您的image \ container之外的.npmrc
)。 一旦本地安装了私有模块,您可以将文件作为构build的一部分复制到映像中:
# Make sure the node_modules contain only the production modules when building this image COPY . /usr/src/app
您还需要确保您的.dockerignore
文件不会排除node_modules
文件夹。
一旦你把文件夹复制到你的镜像中,诀窍就是使用npm rebuild
而不是npm install
。 这将重build任何本地依赖,这是由您的生成服务器和您的docker操作系统之间的任何差异影响:
FROM nodesource/vivid:LTS # For application location, default from nodesource is /usr/src/app # Make sure the node_modules contain only the production modules when building this image COPY . /usr/src/app WORKDIR /usr/src/app RUN npm rebuild CMD npm start