Docker npm install failing - linux

I was using node 10 in my app. I was told by compliance I should use node 16. This was me previous docker file (removing env variables for privacy):
FROM node:10-alpine
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package.json package-lock.json ./
USER node
RUN npm install
RUN npm install express-session
COPY --chown=node:node . .
EXPOSE 8080
CMD [ "node", "app.js" ]
And the build and run process worked with no warning or issues. Now I changed the first line of the docker file for:
FROM node:16.13.0-alpine3.11
RUN npm install -g npm
and now I get a warning about my package-lock.json being created with an older version of npm (which sounds right) and some error on the user not having the permissions to open such file:
I have two questions. Is the package.json file updated automatically or how can I make sure this file is created with the current npm version. And why am I getting the permission issues if the only change is in the node version? I also tried with the command 'npm install -g npm#9.2.0' for the other npm version but it only removed that warning. Why is my contrast not able to be fetched?

I removed the
USER node
line and it did the trick. I am not the creator of the app so I'm not sure on what this line was doing.

Related

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"
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}

The command '/bin/sh -c npm install --silent' returned a non-zero code: 254

When m trying to craete image of react app with dockerenter image description herefile its saying...
The command '/bin/sh -c npm install --silent' returned a non-zero code: 254
Here is my dockerfile
# Pull official base image
FROM node:13.12.0-alpine
# Set working directory
WORKDIR /app
# Add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install --silent
RUN npm install react-scripts#3.4.1 -g --silent
# add app
COPY . ./
# start app
CMD ["npm", "start"]
Make sure that npm is reading package.json from the place you're copying it to.
This basically happens when something is broken while docker tries to install JS packages. As the error code "254" stands for all types of installation failures, the only way to get the specific issue is by running the "install" or "ci" command without --silent.

Run npm update in docker without using the cache on that specific update

Background:
I'm writing code in node.js, using npm and docker. I'm trying to get my docker file to use cache when I build it so it doesn't take too long.
We have a "common" repo that we use to keep logic that is used in a variety of repositories and this gets propagated is npm packages.
The problem:
I want the docker file NOT use the cache on my "common" package.
Docker file:
FROM node:12-alpine as X
RUN npm i npm#latest -g
RUN mkdir /app && chown node:node /app
WORKDIR /app
RUN apk add --no-cache python3 make g++ tini \
&& apk add --update tzdata
USER node
COPY package*.json ./
COPY .npmrc .npmrc
RUN npm install --no-optional && npm cache clean --force
ENV PATH /app/node_modules/.bin:$PATH
COPY . .
package.json has this line:
"dependencies": {
"#myorg/myorg-common-repo": "~1.0.13",
I have tried adding these lines in a variety of places and nothing seems to work:
RUN npm uninstall #myorg/myorg-common-repo && npm install #myorg/myorg-common-repo
RUN npm update #myorg/myorg-common-repo --force
Any ideas on how I can get docker to build and not use the cache on #myorg/myorg-common-repo ?
So I finally managed to solve this using this answer:
What we want to do is invalidate the cache for a specific block in the Docker file and then run our update command. This is done by adding a build argument to the command (CLI or Makefile) like so:
docker-compose -f docker-compose-dev.yml build --build-arg CACHEBUST=0
And then Adding this additional block to the Docker file:
ARG CACHEBUST=1
USER node
RUN npm update #myorg/myorg-common-repo
This does what we want.
The ARG CACHEBUST=1 invalidates the cache and the npm update command runs without it.

npm unable to find correct version of package within Docker

I am attempting to perform npm install within a docker image. As part of the package.json, I need version 1.8.8 of react-pattern-library. Within the docker image, only version 0.0.1 appears to be available.
If I locally run
npm view react-pattern-library versions
I can see version 1.8.8
However the same command within my docker file only show version 0.0.1
Can anyone tell me what configuration setting I need to be able to find the correct version when attempting my docker build?
docker build -t jhutc/molly-ui
Contents of Dockerfile
FROM node:10
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
#COPY package*.json ./
COPY package.json ./
RUN npm set strict-ssl false
ENV HTTP_PROXY="http://proxy.company.com:8080"
ENV HTTPS_PROXY="https://proxy.company.com:8080"
RUN echo $HTTP_PROXY
RUN echo $HTTPS_PROXY
RUN npm view react-pattern-library versions
#RUN npm install
Try deleting the package-lock.json and running npm install again.

Docker permission issue after copying files

I have the following docker file
FROM ubuntu:14.04
#Install Node
RUN apt-get update -y
RUN apt-get upgrade -y
RUN apt-get install nodejs -y
RUN apt-get install nodejs-legacy -y
RUN apt-get install npm -y
RUN update-alternatives --install /usr/bin/node node /usr/bin/nodejs 10
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# COPY distribution
COPY dist dist
COPY package.json package.json
# Substitute dependencies from environment variables
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 8000
And here is the entrypoint script
#!/bin/sh
cp package.json /usr/src/app/dist/
cd /usr/src/app/dist/
echo "starting server"
exec npm start
When I run the image it fails with this error
sh: 1: http-server: not found
npm ERR! weird error 127
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian
I tried various kinds of installation but still get the same error, I also tried checking if the node_modules conatins the http-server executable and it does. I tried forcing 777 permission on all the files but still running into the same error
What could be the problem?
It looks like you're just missing an npm install call somewhere, so the node_modules directory, nor any of its contents (like http-server) are present on the image. After the COPY package.json package.json, if you add a RUN npm install line, that might be all you need.
There are a few other things that could be simpler too though, like you probably don't need an ENTRYPOINT script to run the app and copy package.json since that's already done. Here's a simplified version of a Node Docker image I've been running with. I'm using the base Node images which, I believe, are Linux-based, but you could probably keep the Ubuntu stuff if you wanted to and it shouldn't be an issue.
FROM node:6.9.5
# Create non-root user to run app with
RUN useradd --user-group --create-home --shell /bin/bash my-app
# Set working directory
WORKDIR /home/my-app
COPY package.json ./
# Change user so that everything that's npm-installed belongs to it
USER my-app
# Install dependencies
RUN npm install --no-optional && npm cache clean
# Switch to root and copy over the rest of our code
# This is here, after the npm install, so that code changes don't trigger an un-caching
# of the npm install line
USER root
COPY .eslintrc index.js ./
COPY app ./app
RUN chown -R my-app:my-app /home/my-app
USER my-app
CMD [ "npm", "start" ]
It's good practice to make a specific user for owning/running your code and not using root, but, as I understand it, you need to use root to put files onto your image, hence the switching users a couple times here (which is what USER ... does).
I'll also note that I use this image with Docker Compose for local development, which is what the comment about code changes is referring to.

Resources