Testing local npm package using Docker - node.js

I have a local npm package which I need to build and run npm link inside Docker container. I have these line in my Dockerfile
WORKDIR /usr/src/app
COPY . .
RUN npm ci && npm run build && npm link
How can I test this npm package outside of Docker container ?

Related

laravel 8 on Docker Vite manifest not found at: ...\public\/build/manifest.json with

i have problem to do npm run dev on docker on laravel project, i am using vue js authentication and i need to do npm run dev on docker and i need help
here is the content of my Dockerfile
FROM node:14-alpine as node
WORKDIR /var/www
COPY . .
RUN npm install --global cross-env
RUN npm install
RUN npm run build`
EXPOSE 3000
VOLUME /var/www/node_modules
here is the content of my docker-compose.yml
node:
build:
context: .
target: node
volumes:
- .:/usr/src
- ./node_modules:/usr/src/node_modules
command: npm run dev
tty: true
how should i go about solving this problem npm install && npm run dev ?
thank you in advance for your help

Whenever performing docker build, always get dependency error

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.

Docker Node Sass does not yet support your current environment

FROM node:latest as nodeBuilder
RUN npm rebuild node-sass
WORKDIR /var/www/app
COPY ./webapp /var/www/app
RUN npm install
RUN npm run build
RUN rm -rf node_module
when i run this got this error
Error: Node Sass does not yet support your current environment: Linux 64-bit with Unsupported runtime (88)
For more information on which environments are supported please see:
https://github.com/sass/node-sass/releases/tag/v4.14.1
i use
FROM node:12.22.1 as nodeBuilder
WORKDIR /var/www/app
COPY ./backend /var/www/app
RUN rm -rf node_module
RUN npm install
RUN npm rebuild node-sass
RUN npm run build
RUN rm -rf node_module
and it's work because of node v15 dose not support node_sass

Docker node Error: 'darwin-x64' binaries cannot be used on the 'linux-x64' platform

I am trying to run a container based on my app image by using docker and i getting the next error:
/app/node_modules/sharp/lib/libvips.js:68
throw new Error('${vendorPlatformId}' binaries cannot be used on the '${currentPlatformId}' platform. Please remove the 'node_modules/sharp' directory and run 'npm install' on the '${currentPlatformId}' platform.);
Error: 'darwin-x64' binaries cannot be used on the 'linux-x64' platform. Please remove the 'node_modules/sharp' directory and run 'npm install' on the 'linux-x64' platform.
My Dockerfile:
FROM node:10.20.1
WORKDIR /app
COPY package.json .
RUN npm install -g node-gyp
RUN npm install
EXPOSE 8080
CMD [ "npm", "start" ]
I tryied to add the next Commands to my Dockerfile but i am getting the same error:
Second attempt Dockerfile:
FROM node:10.20.1
WORKDIR /app
COPY package.json .
RUN npm install -g node-gyp
RUN rm -rf node_modules/sharp
RUN npm install --arch=x64 --platform=linux --target=8.10.0 sharp
EXPOSE 8080
CMD [ "npm", "start" ]
The docker command I using locally on my mac in order to run that container is:
docker run -d -p 8080:8080 --env-file ./.env --name server-dev-1 server:1
Any ideas?

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

Resources