我有一个在Docker容器中运行的应用程序。它需要来自公司的私有NPM注册表(Sinopia)的一些私有模块,访问这些模块需要用户身份验证。Dockerfile文件是FROM iojs:latest。
我试过了:
1)在项目根目录中创建一个.npmrc文件,这实际上没有区别,npm似乎忽略了它。2)使用NPM_CONFIG_REGISTRY、NPM_CONFIG_USER等的环境变量,但是用户没有登录。
从本质上讲,我似乎无法在docker build进程中对用户进行身份验证。我希望有人可能已经遇到了这个问题(看起来是一个足够明显的问题),并有一个很好的方法来解决它。
(最重要的是,我在Docker Hub上使用了Automated Builds (在推送时触发),这样我们的服务器就可以使用预先构建的镜像访问私有的Docker注册表。)
有没有好的方法: 1)在构建时为NPM注入凭证(这样我就不必将凭证提交到我的Dockerfile),或者2)用另一种我没有想到的方式来做这件事?
发布于 2015-06-23 06:15:53
在为node.js / io.js容器(you/iojs)创建基础镜像时,我发现了一种比较优雅的解决方案:
的.npmrc文件
示例.npmrc
registry=https://npm.mydomain.com/
username=dockerUser
email=docker@mydomain.com
strict-ssl=false
always-auth=true
//npm.mydomain.com/:_authToken="someAuthToken"文件.npmrc的Dockerfile
下面是我的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" ]FROM you/iojs,您就可以开始工作了。发布于 2020-09-22 23:15:10
在2020年,我们将推出BuildKit。你不必再通过COPY或ENV传递秘密了,因为它被认为是不安全的。
示例Dockerfile
# syntax=docker/dockerfile:experimental
FROM node:13-alpine
WORKDIR /app
COPY package.json yarn.lock ./
RUN --mount=type=ssh --mount=type=secret,id=npmrc,dst=$HOME/.npmrc \
yarn install --production --ignore-optional --frozen-lockfile
# More stuff...然后,您的build命令可以如下所示:
docker build --no-cache --progress=plain --secret id=npmrc,src=/path-to/.npmrc .发布于 2016-08-05 13:59:37
对于那些通过google找到这篇文章的人来说,他们仍然在寻找一种替代方法,不需要在docker图像和容器上留下私有的npm令牌:
我们能够通过在docker build之前执行npm install来实现这一点(通过这样做,您可以将.npmrc放在映像容器之外)。在本地安装私有模块后,您可以将文件复制到映像中,作为构建的一部分:
# 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。这将重新构建受构建服务器和docker OS之间的任何差异影响的任何本机依赖项:
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 starthttps://stackoverflow.com/questions/30573501
复制相似问题