Whenever performing docker build, always get dependency error - node.js

When ever I perform docker build I get this:
Sending build context to Docker daemon 263.5MB
Step 1/19 : FROM node:alpine3.16 AS development
---> 789fb8adc830
Step 2/19 : WORKDIR /usr/src/app
---> Using cache
---> 75ce41f126cc
Step 3/19 : COPY --chown=node:node package*.json ./
---> 802474abc3db
Step 4/19 : RUN npm ci
---> Running in 74111097fd82
npm WARN ERESOLVE overriding peer dependency
npm WARN While resolving: ajv-keywords#3.5.2
npm WARN Found: peer ajv#"^6.9.1" from the root project
npm WARN
npm WARN Could not resolve dependency:
npm WARN peer ajv#"^6.9.1" from the root project
npm ERR! code EAI_AGAIN
npm ERR! syscall getaddrinfo
npm ERR! errno EAI_AGAIN
npm ERR! request to https://registry.npmjs.org/ajv failed, reason: getaddrinfo EAI_AGAIN registry.npmjs.org
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2022-09-10T08_30_14_350Z-debug-0.log
The command '/bin/sh -c npm ci' returned a non-zero code: 1
this is the docker file used to build the image
FROM node:alpine3.16 AS development
# Create app directory
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image.
# A wildcard is used to ensure copying both package.json AND package-lock.json (when available).
# Copying this first prevents re-running npm install on every code change.
COPY --chown=node:node package*.json ./
# Install app dependencies using the `npm ci` command instead of `npm install`
RUN npm ci
# Bundle app source
COPY --chown=node:node . .
# Use the node user from the image (instead of the root user)
USER node
FROM node:alpine3.16 AS build
WORKDIR /usr/src/app
COPY --chown=node:node package*.json ./
# In order to run `npm run build` we need access to the Nest CLI which is a dev dependency. In the previous development stage we ran `npm ci` which installed all dependencies, so we can copy over the node_modules directory from the development image
COPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modules
COPY --chown=node:node . .
# Run the build command which creates the production bundle
RUN npm run build
# Set NODE_ENV environment variable
ENV NODE_ENV production
# Running `npm ci` removes the existing node_modules directory and passing in --only=production ensures that only the production dependencies are installed. This ensures that the node_modules directory is as optimized as possible
RUN npm ci --only=production --omit=dev && npm cache clean --force
USER node
FROM node:alpine3.16 AS production
# Copy the bundled code from the build stage to the production image
COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules
COPY --chown=node:node --from=build /usr/src/app/dist ./dist
# Start the server using the production build
CMD [ "node", "dist/main.js" ]
How can I resolve the issue? this issue never occurs when the service is built and executed in VSCode

Most likely you are encountering this issue: https://github.com/npm/cli/issues/4998.
Using either --force or --legacy-peer-deps option should do the trick for you, for now.

Related

unable to run npm install in docker image (getting auth error)?

I have a very simple Dockerfile
FROM node:17.3.1 as build
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
RUN npm run build
FROM node:17.3.1
WORKDIR /app
COPY package.json .
RUN npm install --only=production
COPY --from=build /app/dist ./dist
CMD npm run start:prod
When running docker build -t nestjs-hello-world . I am getting the following error. I don't understand why it is needing to login to npm. It is using the default registry. Even tried specifying the default registry as part of the npm install command, just to make sure, but same error..
=> ERROR [build 4/6] RUN npm install 8.3s
------
> [build 4/6] RUN npm install:
#8 8.230 npm notice
#8 8.231 npm notice New minor version of npm available! 8.1.2 -> 8.3.1
#8 8.231 npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.3.1>
#8 8.232 npm notice Run `npm install -g npm#8.3.1` to update!
#8 8.233 npm notice
#8 8.235 npm ERR! code E401
#8 8.237 npm ERR! Unable to authenticate, your authentication token seems to be invalid.
#8 8.238 npm ERR! To correct this please trying logging in again with:
#8 8.239 npm ERR! npm login
#8 8.251
#8 8.252 npm ERR! A complete log of this run can be found in:
#8 8.252 npm ERR! /root/.npm/_logs/2022-01-20T00_08_48_935Z-debug.log
Any ideas why this is happening for me please?
Thanks
Thanks to Phil I had the same problem and I managed to unblock my problem thanks to your comment
Here is my Dockerfile
FROM node:16
# 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+)
# copying packages first helps take advantage of docker layers
COPY package.json .
RUN npm install prettier -g
# If you are building your code for production
RUN npm install
# Bundle app source
COPY . .
RUN npm run build
EXPOSE 4000
CMD [ "node", "dist/server.js" ]

node-pre-gyp not accessible from fsevents

I am getting the following error when deploying my container
#15 1.066 npm ERR! node-pre-gyp not accessible from fsevents
#15 1.076
#15 1.076 npm ERR! A complete log of this run can be found in:
#15 1.076 npm ERR! /root/.npm/_logs/2022-01-11T11_40_13_120Z-debug.log
------
executor failed running [/bin/sh -c npm ci]: exit code: 1
following is my dockerfile
WORKDIR /app
COPY package*.json ./
COPY .npmrc .npmrc
RUN npm set progress=false
RUN npm ci
RUN npm audit
COPY . .
RUN npm run build
FROM nginx
RUN mkdir /app
COPY --from=0 /app/dist /app
COPY ./__docker_content_start.sh /start.sh
RUN chmod +x /start.sh
COPY nginx.conf /etc/nginx/nginx.conf
CMD /start.sh
I am unable to find a solution for this. Any help appreciated Thanks
Add node-pre-gyp as a dependency to your package.json
npm i --save node-pre-gyp

" xxx is not defined no-undef" on Docker build command out of sudden

I have a simple ReactJS application, which is deployed to AWS EB, with Docker.
My Dockerfile looks like this:
FROM node:12.2.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
ENV REACT_APP_VAR val
ENV REACT_APP_VAR2 val2
COPY package.json /app/package.json
RUN npm install --silent --unsafe-perm --max-old-space-size=1024
RUN npm install react-scripts#3.0.1 -g --silent --max-old-space-size=1024
COPY . /app
RUN npm --max-old-space-size=1024 run build
FROM nginx:1.16.0-alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
I have been using the same docker file for months by now. Today suddenly, I start build errors like below on AWS EB Logs (and got the same error while trying to build the image on my local)
Step 21/26 : RUN npm --max-old-space-size=1024 run build
---> Running in 879c536e7f65
> frontend#0.1.0 build /app
> react-scripts build
Creating an optimized production build...
Failed to compile.
./src/components/file.js
Line 187:5: 'SomeCustomButtonRef' is not defined no-undef
Search for the keywords to learn more about each error.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! frontend#0.1.0 build: `react-scripts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the frontend#0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-02-23T10_25_14_373Z-debug.log
This error started happening today, out of sudden.
When I run the command npm --max-old-space-size=1024 run build on my local w/o any Docker related commands, it runs fine and builds.
I have tried to change the node version tag in Dockerfile, but the result was the same. I have tried to comment out the part where it breaks (it is simply SomeCustomButtonRef = React.createRef()) but then docker build command breaks with more lines ABC is not defined no-undef in some other file.
Any one had similar issue with react + docker, especially like out of sudden it starts breaking the builds ?
npm has two files that explain what packages need to be installed. package.json lists out the package and potentially broad version constraints; package-lock.json has extremely specific versions of the packages that are used in a specific build. When you do the initial install inside your Dockerfile, you should copy both of these files into the image:
COPY package.json package-lock.json .
RUN npm ci
The problem you describe doesn't sound like a problem with Docker or Node itself, but one of the packages that's being used in the npm run build sequence. Since the original Dockerfile didn't include the package-lock.json file, if some involved package updated from version 1.2.3 to 1.2.4 and the newer version had an issue, everything would look consistent until the build eventually failed. Including the lock file keeps you on a known-working build chain.

Copy output of npm install to docker container

I dockerized node.js and all works fine
Dockerfile:
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
EXPOSE 9000
CMD ["npm", "run", "dev"]
I'm trying to run npm install outside Dockerfile and to copy content of npm install to docker container
On docker host i ran
npm install --prefix /opt/npm/ -g
Folder /opt/npm/lib/node_modules/ui is created. In that folder there are bunch json files and folder node_modules.Dockerfile is in that foler.Now, in Dockerfile i skipped npm install and just copied content of /opt/npm/lib/node_modules/ui to docker container.
Modified Dockerfile
FROM node:alpine
WORKDIR '/app'
COPY . .
EXPOSE 9000
Built image from Dockerfile sucessfully, but when trying to run container from that image
docker run -p 9000:4200 pm
> ui#0.0.0 dev /app
> ng serve --host 0.0.0.0 --proxy-config src/proxy.conf.json
sh: ng: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! ui#0.0.0 dev: `ng serve --host 0.0.0.0 --proxy-config src/proxy.conf.json`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the ui#0.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
Is it possible to run npm install outside docker container ?
When dockerizing any application you should always compile and install dependencies in the docker container.
Your Docker file start form node:alpine. this means that when you install an npm package that needs compilation outside (your OS) the alpine OS won't be able to use this.
Best practice is to always build your application on the same OS. That's way docker introduce build container.
# Dockerfile
FROM node:12.13-alpine As build
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
FROM node:12.13-alpine as production
WORKDIR /usr/src/app
COPY ./ ./ # copy static files
COPY --from=build /usr/src/app/node_modules ./. # Copy node_modules from build container
EXPOSE 3000
CMD ["node", "main.js"]
# .dockerignore
node_modules
Dockerfile
Try to fit this to your environment

Docker container that pulls from private gilab repository

I'm building a Docker container for my Node.js + Vue application.
Since I have a global css library in another repository I have added this line in my package.json file:
"lib-css": "git+ssh://git#git.lib.com:9922/username/lib-css.git#development",
That way when I run npm install I install also my CSS library. The problem is that on my local env it asks for my password and I can insert it, but in the Docker build the process fails with the following error:
Step 7/10 : RUN npm install
---> Running in db10ca83586d
npm WARN deprecated babel-preset-es2015#6.24.1: 🙌 Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!
npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ssh://git#git.lib.com:9922/username/lib-css.git
npm ERR!
npm ERR! Host key verification failed.
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
npm ERR!
npm ERR! exited with error code: 128
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2017-12-11T08_49_11_152Z-debug.log
This is my current Dockerfile:
FROM node:carbon
WORKDIR /usr/src/app
RUN mkdir -p /root/.ssh
COPY .secrets /root/.ssh/id_rsa
RUN chmod 700 /root/.ssh && chmod 600 /root/.ssh/*
# 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 ./
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
# Bundle app source
COPY . .
EXPOSE 8081
CMD [ "npm", "run dev" ]
~
My .secrets file contains my private key associated to the repository.
How can I make this works?

Resources