Docker npm errno -13 - docker

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

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?

permission denied in Docker container

I am new to Docker and I am trying to run a container following this tutorial and was running into some issues while using the --volume or -v as shown below:
docker run -v <absolute path to host directory>:<absolute path to container working directory> <image id>
So mine looks like this:
docker run -p 3000:3000 -v /usr/app/node_modules -v "$(pwd)":/usr/app 900a3497fc39
The error thrown is this:
npm ERR! code EACCES
npm ERR! syscall open
npm ERR! path /usr/app/package.json
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, open '/usr/app/package.json'
npm ERR! [Error: EACCES: permission denied, open '/usr/app/package.json'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'open',
npm ERR! path: '/usr/app/package.json'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-11-12T13_39_38_626Z-debug.log
I have built the image without any issues:
docker build -f Dockerfile.dev .
Sending build context to Docker daemon 23.55kB
Step 1/6 : FROM node
---> f1974cfde44f
Step 2/6 : WORKDIR /usr/app
---> Using cache
---> 120da5dd5fa4
Step 3/6 : COPY ./package.json ./
---> Using cache
---> 040ba41014c8
Step 4/6 : RUN npm install
---> Using cache
---> 96315e12e5d0
Step 5/6 : COPY . .
---> db93b8725037
Step 6/6 : CMD [ "npm", "run", "dev" ]
---> Running in 9c9d58318420
Removing intermediate container 9c9d58318420
---> 900a3497fc39
Successfully built 900a3497fc39
My Dockerfile.dev looks like this:
FROM node
WORKDIR /usr/app
COPY ./package.json ./
RUN npm install
COPY . .
CMD [ "npm", "run", "dev" ]
And the package.json looks like this:
{
"name": "vite-counter",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"vue": "^3.0.0-beta.15"
},
"devDependencies": {
"vite": "^1.0.0-beta.3",
"#vue/compiler-sfc": "^3.0.0-beta.15"
}
}
From what I understand, I do not have sufficient permissions in the container to run this. This is exactly what I was following in the tutorial. What am I doing wrong and how do I fix this?

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

Getting an error on dockerising nest.js application

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.

Resources