Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github private packages are not installing in docker build

I have an npm package hosted on github. It's working fine while working on the local machine. But it's not installing while the docker build process.

Dockerfile

FROM node:14-alpine as base
EXPOSE 8080
ARG LIB_GITHUB_TOKEN
ENV NODE_ENV=production \
LIB_GITHUB_TOKEN="${LIB_GITHUB_TOKEN}"
WORKDIR /home/node/app
RUN chown -R node:node /home/node/app
COPY --chown=node:node package.json package-lock*.json tsconfig.json .npmrc ./
RUN npm install --only=development
RUN ls -a **every pkgs installed except gihub pvt pkg** 
RUN cd node_modules/ && ls
RUN npm run build

.npmrc file

registry=https://registry.npmjs.org/
@github_user:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=${LIB_GITHUB_TOKEN}

Every packages are installed except my GitHub private package.

Same working properly with docker-compose.

If I add npm ci --only=production in dockerfile. it gives -

npm ERR! code E401
npm ERR! Unable to authenticate, need: Basic realm="GitHub Package Registry"
like image 321
Arpit Yadav Avatar asked Apr 09 '26 02:04

Arpit Yadav


1 Answers

I tried using environment variables with .npmrc and I ran into the same 401 error you're experiencing. Instead of copying .npmrc into the image I added it to .dockerignore

And then do this in your Dockerfile

ARG github_token
ENV GITHUB_TOKEN=$github_token

RUN echo registry=https://registry.npmjs.org/ >> ~/.npmrc
RUN echo @github_user:registry=https://npm.pkg.github.com/ >> ~/.npmrc
RUN echo //npm.pkg.github.com/:_authToken=$GITHUB_TOKEN >> ~/.npmrc

And to build:

docker build . --build-arg github_token=${GITHUB_TOKEN}
like image 145
tylermmorton Avatar answered Apr 11 '26 17:04

tylermmorton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!