Getting an error on dockerising nest.js application - docker

I am working on a Nest.js application and this is the Dockerfile that we have. When I run this I am getting an error on the npm run build step in docker.
This is the build task in package.json.
"build": "nest build"
sh: nest: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! atom-qbuilder-api#0.0.1 build: `nest build`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the atom-qbuilder-api#0.0.1 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Dockerfile
# Build
FROM node:8-alpine as builder
COPY package.json /usr/src/atom/package.json
COPY package-lock.json /usr/src/atom/package-lock.json
WORKDIR /usr/src/atom
# Install dependencies
RUN npm install --production --loglevel warn
ARG APPLICATION
ARG BRANCH
ARG BUILD_NUMBER
ARG SHA
ENV NODE_ENV qa
ENV VERSION 1.0.0
## Copy app directory in container and set workdir
COPY . /usr/src/atom
# Build & Test Coverage
RUN set -x \
&& npm run build \
&& echo "{ \"status\": \"Ok\", \"result\": { \"version\": \"$VERSION\", \"branchName\": \"$BRANCH\", \"SHA\": \"$SHA\", \"buildDate\": \"$(date)\", \"buildNumber\": \"$BUILD_NUMBER\", \"environment\": \"$NODE_ENV\" } }" > build.json
# Release
FROM node:8-alpine
ENV PORT 3000
COPY --from=builder /usr/src/atom/node_modules /usr/src/atom/node_modules
COPY --from=builder /usr/src/atom/dist /usr/src/atom/dist
COPY --from=builder /usr/src/atom/package.json /usr/src/atom/package.json
COPY --from=builder /usr/src/atom/build.json /usr/src/atom/build.json
WORKDIR /usr/src/atom
EXPOSE $PORT
## Run supervisor as foreground process
CMD bash -c "npm run start:prod && /usr/bin/supervisord"

#nestjs/cli (where the nest command comes from) is by default in devDependencies. Unless you've modified your deps in package.json, running npm i --production will not install #nestjs/cli which will lead to the error you currently have. You can either use a multi-staged dockerfile or move #nestjs/cli to your dependencies instead of devDeps.

Related

Can build but not run Dockerfile: Could not find a required file

I'm trying to run an react project with a Docker.
I build a docker's image, but when i run it I've got the following message:
> react#1.0.0 start /app
> react-scripts start
Could not find a required file.
Name: index.html
Searched in: /app/public
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! react#1.0.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the react#1.0.0 start 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/2022-09-13T13_09_01_107Z-debug.log
I don't understand the error, but i do have a index.html file at ./public/index.html
Here is my Dockerfile:
FROM node:14-alpine
WORKDIR /app
COPY . /app
RUN npm install
COPY . app/
EXPOSE 3090
CMD ["npm", "start"]
EDIT:
I changed my Dockerfile for the following:
FROM node:14-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3090
CMD ["npm", "start"]
RUN ls -la /app
RUN ls -la /app/public
Now i Have the error:
=> ERROR [7/7] RUN ls -la /app/public 0.4s
------
> [7/7] RUN ls -la /app/public:
#11 0.360 ls: /app/public: No such file or directory
------
executor failed running [/bin/sh -c ls -la /app/public]: exit code: 1
I guess it's because i didn't had copy, but i still have the error even if I had the following line :
COPY ./public /app
You could temporarily add a RUN ls -la /app or RUN ls -la /app/public to your Dockerfile. This allows you to see all the directories/files copied into the container.
There is also a possibility that a dockerignore file ignores the files you want to copy.
How do you add items to .dockerignore?

Docker npm errno -13

I'm running a very simple Node.js Koa "hello world" application on Docker 2.4.0 on Windows. If the base image is node:10-alpine, it works well:
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 ./
USER node
RUN npm install
COPY --chown=node:node . .
EXPOSE 8080
CMD [ "node", "index.js" ]
But if I change the version to FROM node:15.0.1-alpine3.10
Step 6/9 : RUN npm install
Removing intermediate container 6ff212db1484
---> bfde467f103d
Step 6/9 : RUN npm install
---> Running in 37ef90ef4039
npm ERR! code EACCES
npm ERR! syscall open
npm ERR! path /home/node/app/package-lock.json
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, open '/home/node/app/package-lock.json'
npm ERR! [Error: EACCES: permission denied, open '/home/node/app/package-lock.json'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'open',
npm ERR! path: '/home/node/app/package-lock.json'
npm ERR! }
........
npm install will try to update the package-lock.json file with the current actual versions of the packages it's installed. For installation in a Docker image, you don't really want this; use npm ci instead to avoid this.
The other problem you're encountering is that COPY by default makes files owned by the root user, but you've switched to an alternate "node" user. You probably want the application code in your image to be owned by root, and then run as an alternate user: if there's some sort of security issue, this gives you an extra layer of protection against the code in the container getting modified.
If you do both of these things, the corrected Dockerfile would roughly look like:
FROM node:15.0.1-alpine3.10
# WORKDIR also creates the directory. It will be owned by root,
# which is probably what you want. (So no `RUN mkdir ...`.)
WORKDIR /home/node/app
# Stay as the root user for now.
# Install packages:
COPY package*.json ./
RUN npm ci # not `npm install`
# Copy in the rest of the application (still owned by root):
COPY . .
# Declare runtime metadata. Only now switch to the "node" user.
# This will not be able to modify the source code (good).
USER node
EXPOSE 8080
CMD ["node", "index.js"]

docker container with Nestjs app fail to start

Hi I'm beginner with Docker
I use the Docker Desktop on Windows (Linux Containers) to manage containers and I need to create a container with a NestJs application.
I built the image containing the app but when I want to run a container with this image I get this error:
> helloworld#0.0.1 start /usr/src/app
> nest start
/usr/src/app/node_modules/.bin/nest: line 1: ../#nestjs/cli/bin/nest.js: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! helloworld#0.0.1 start: `nest start`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the helloworld#0.0.1 start 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/2020-05-12T09_10_04_973Z-debug.log
my Dockerfile is like this
FROM node:current-slim
WORKDIR /usr/src/app
RUN npm i -g #nestjs/cli
COPY package.json /usr/src/app/package.json
RUN npm install
COPY . .
EXPOSE 3000
CMD ["sh", "-c", "npm run start"]
With this dockerfile it's working on Linux but on Windows I get the error.
I tried to use nestjs official docker image instead of node but I get same error.

Access Private Repositories from docker container as a npm dependency

I am trying inject my ssh on build process
docker build --ssh default=C:\Users\***\.ssh\id_rsa .
// package.json
"airbase-common": "git+ssh://git#bitbucket.org/******/airbase-common.git"
// error
npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ssh://git#bitbucket.org/******/airbase-common.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.
Thanks in advance
I found solution:
If anyone as i am bulding his image in windows environment you should to keep .ssh folder as neighbor your Dockerfile
// Dockerfile
FROM node:12.16 AS builder
WORKDIR /app
COPY ./package.json ./
RUN mkdir /root/.ssh/
COPY .ssh /root/.ssh
RUN chmod 700 /root/.ssh/
RUN chmod 644 /root/.ssh/id_rsa.pub
RUN chmod 600 /root/.ssh/id_rsa
VOLUME [ "/root/.ssh" ]
RUN npm install
.... etc

Data created in Dockerfile does not exist within container

I appreciate your help!
I have the following Dockerfile:
FROM node:slim
RUN mkdir -p /project
WORKDIR /project
# workdir is empty
RUN ls -a
COPY . /project
# workdir is full
RUN ls -a
# this command create node_modules folder on it own
RUN npm install
# I have node_modules folder with packages
RUN ls -a
RUN ls -d node_modules/*
Last outputs are:
Step 8/9 : RUN ls -a
---> Running in 90d8793f491a
.
..
.babelrc
.editorconfig
.eslintignore
.eslintrc.js
.git
.gitignore
.postcssrc.js
Dockerfile-dev
README.md
build
config
docker-compose.dev.yml
index.html
node_modules
package-lock.json
package.json
src
static
test
Removing intermediate container 90d8793f491a
---> b563d272b270
Step 9/9 : RUN ls -d node_modules/*
---> Running in a14bb4024e5e
node_modules/#babel
node_modules/#types
node_modules/abab
node_modules/abbrev
node_modules/accepts
node_modules/acorn
But when I look at the container's files from the following command:
docker-compose -f docker-compose.dev.yml run project ls
I have no node_modules folder (folder with dependencies) and my container cannot start because of it:
docker-compose -f docker-compose.dev.yml up
sh: 1: webpack-dev-server: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! project#1.0.0 dev: `webpack-dev-server --inline --progress --config build/webpack.dev.conf.js`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the project#1.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?
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-04-11T19_09_35_669Z-debug.log
The docker-compose is simple:
version: '3'
services:
project:
container_name: project
build:
context: .
dockerfile: Dockerfile-dev
command: npm run dev
volumes:
- .:/project
ports:
- 8080:8080
The questions are: why it happens? How to solve the problem?
why it happens?
https://github.com/moby/moby/issues/4361#issuecomment-167156818
How to solve the problem?
https://github.com/moby/moby/issues/4361#issuecomment-210231967

Resources