在Docker中使用私有npm存储库

de90aj5v  于 2023-03-17  发布在  Docker
关注(0)|答案(2)|浏览(136)

我们有一个使用sinopia托管的私有npm存储库,它有基本的auth凭据。我们的应用程序使用私有存储库的npm包。我创建了验证令牌并尝试了它,但在RUN npm install utilities@0.1.9行遇到错误:

npm ERR! code E403
npm ERR! 403 Forbidden: utilities@0.1.9
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-07-05T10_55_25_752Z-debug.log

我的Dockerfile是:

FROM keymetrics/pm2:latest-alpine
RUN mkdir -p /app
WORKDIR /app
COPY package.json ./
COPY .npmrc ./
RUN npm config set registry http://private.repo/:_authToken=CqgPS5l++vjD0n6ynxrVNg==.
RUN npm install utilities@0.1.9
RUN apk update && apk add yarn python g++ make && rm -rf /var/cache/apk/*
RUN set NODE_ENV=production
RUN npm config set registry https://registry.npmjs.org/
RUN npm install
COPY . /app
RUN ls -al -R
EXPOSE 51967
CMD [ "pm2-runtime", "start", "pm2.json" ]

错误日志为:

11 verbose stack     at tryCatcher (/usr/local/lib/node_modules/npm/node_modules/bluebird/js/release/util.js:16:23)
11 verbose stack     at Promise._settlePromiseFromHandler (/usr/local/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:512:31)
11 verbose stack     at Promise._settlePromise (/usr/local/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:569:18)
11 verbose stack     at Promise._settlePromise0 (/usr/local/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:614:10)
11 verbose stack     at Promise._settlePromises (/usr/local/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:693:18)
11 verbose stack     at Async._drainQueue (/usr/local/lib/node_modules/npm/node_modules/bluebird/js/release/async.js:133:16)
11 verbose stack     at Async._drainQueues (/usr/local/lib/node_modules/npm/node_modules/bluebird/js/release/async.js:143:10)
11 verbose stack     at Immediate.Async.drainQueues (/usr/local/lib/node_modules/npm/node_modules/bluebird/js/release/async.js:17:14)
11 verbose stack     at runCallback (timers.js:794:20)
11 verbose stack     at tryOnImmediate (timers.js:752:5)
11 verbose stack     at processImmediate [as _immediateCallback] (timers.js:729:5)

有人能帮我吗?

ubbxdtey

ubbxdtey1#

我猜包utilities@0.1.9是你的私有包?如果是的话,看起来你的auth令牌要么没有被使用,要么由于某种原因没有访问那个包。
您可以尝试编写~/.npmrc文件,而不是使用配置集,这只是使用的一种情况:

RUN echo -e "//private.repo/:_authToken=... > ~/.npmrc

这将导致你的docker用户使用这个令牌在定义的注册表中进行身份验证,这就是我们在大多数情况下为npm设置auth令牌的方法。
另外,您可能需要考虑不要一个接一个地使用多个RUN命令。这会导致为每个命令创建一个新的图像层,并且可能会使容器的大小急剧膨胀。请尝试在命令末尾使用&& \,然后将下一个命令放在不带RUN位的新行中。例如:

FROM keymetrics/pm2:latest-alpine

RUN mkdir -p /app

WORKDIR /app

COPY package.json ./
COPY .npmrc ./

RUN npm config set registry http://private.repo/:_authToken=$AUTH_TOKEN && \
  npm install utilities@0.1.9 && \
  apk update && apk add yarn python g++ make && rm -rf /var/cache/apk/* && \
  set NODE_ENV=production && \
  npm config set registry https://registry.npmjs.org/ && \
  npm install

COPY . /app

RUN ls -al -R

EXPOSE 51967

CMD [ "pm2-runtime", "start", "pm2.json" ]

它应该是一样的可读性,但最终的图像应该更小,并可能有点快的建设。

abithluo

abithluo2#

我的解决方案位于dockerfile集配置中

ENV NPM_CONFIG_REGISTRY=https://privatedomain.com
ENV NPM_CONFIG_USERNAME=username
ENV NPM_CONFIG_EMAIL=my@email.com
ENV NPM_CONFIG_PASSWORD=my password

RUN npm set registry https://privatedomain.com

Docker版本20.10.7,构建版本20.10.7- 0 ubuntu 5 ~21.04.2节点版本17.0.1

更新

经过几个小时的努力,我终于把它解开了

FROM node:18-alpine3.15

ARG NPM_REGISTRY
ARG NPM_TOKEN
ARG NPM_CONFIG_REGISTRY

ENV NPM_DOCKER_REGISTRY=$NPM_REGISTRY
ENV NPM_CONFIG_REGISTRY=$NPM_CONFIG_REGISTRY
ENV NPM_DOCKER_TOKEN=$NPM_TOKEN
ENV WORKDIR=/app

WORKDIR ${WORKDIR}
    
COPY . ${WORKDIR}
    
RUN echo "//${NPM_DOCKER_REGISTRY}/:_authToken="'"'${NPM_DOCKER_TOKEN}'"' > ${WORKDIR}/.npmrc
    
RUN npm i --yes
RUN npm run build

RUN rm -fr ${WORKDIR}/src
RUN rm -fr ${WORKDIR}/.npmrc

CMD [ "npm", "run", "start" ]

并执行构建

NPM_REGISTRY=url NPM_TOKEN=token docker build -t name/xxx:node-alpine . \
  --build-arg NPM_CONFIG_REGISTRY=https://${NPM_REGISTRY} \
  --build-arg NPM_REGISTRY=${NPM_REGISTRY} \
  --build-arg NPM_TOKEN=${NPM_TOKEN}

相关问题