permission denied in Docker container - docker

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?

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.

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.

How to install bcrypt on docker image?

I am having issue installing bcrypt on my docker.
my node application need bcrypt do encrypt user data. I used npm install bcrypt --save to install on my local computer and this seem to be working well. Now I want to dockerize my application.
My Dockerfile look like this.
FROM node:10.12.0
RUN npm install --global node-pre-gyp
RUN apt-get update && apt-get install -y build-essential && apt-get install -y python && npm install
RUN mkdir -p /usr/src/app
RUN whoami
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN npm install nodemon -g --save
RUN npm install
RUN node -v
RUN npm install bcrypt -g --save
# RUN npm install bcrypt - g --save
# CMD [ 'pm2', 'start', 'app.js' '--log-date-format' "'YYYY-MM-DD HH:mm Z'" ]
CMD [ "nodemon" ]
my package.json file looks like this:
{
"name": "api",
"version": "1.0.0",
"description": "Best app",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "name",
"license": "ISC",
"dependencies": {
"bcrypt": "^3.0.2",
"config": "^2.0.1",
"dotenv": "^6.1.0",
"express": "^4.16.4",
"joi": "^14.0.0",
"joi-objectid": "^2.0.0",
"jsonwebtoken": "^8.3.0",
"lodash": "^4.17.11",
"mongoose": "^5.3.4",
"multer": "^1.4.1",
"nodemailer": "^4.7.0",
"nodemon": "^1.18.4",
"stripe": "^6.15.0"
}
}
When I try to build with docker build .
This error occurs:
Sending build context to Docker daemon 226 MB
Step 1/12 : FROM node:10.12.0
---> a2b9536415c2
Step 2/12 : RUN npm install --global node-pre-gyp
---> Using cache
---> c76403d98dd5
Step 3/12 : RUN apt-get update && apt-get install -y build-essential && apt-get install -y python && npm install
---> Using cache
---> 7926c2a74e90
Step 4/12 : RUN mkdir -p /usr/src/app
---> Using cache
---> 5ecb4eb08f05
Step 5/12 : RUN whoami
---> Using cache
---> e5ff9b23c652
Step 6/12 : WORKDIR /usr/src/app
---> Using cache
---> d0f9dd01719b
Step 7/12 : COPY . /usr/src/app
---> 30d30749e5d4
Removing intermediate container a20cfaca9bb3
Step 8/12 : RUN npm install nodemon -g --save
---> Running in a495bc73977f
/usr/local/bin/nodemon -> /usr/local/lib/node_modules/nodemon/bin/nodemon.js
> nodemon#1.18.7 postinstall /usr/local/lib/node_modules/nodemon
> node bin/postinstall || exit 0
Love nodemon? You can now support the project via the open collective:
> https://opencollective.com/nodemon/donate
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents#1.2.4 (node_modules/nodemon/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents#1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
+ nodemon#1.18.7
added 223 packages from 130 contributors in 12.461s
---> 9bb36a08bd55
Removing intermediate container a495bc73977f
Step 9/12 : RUN npm install
---> Running in f544752d8d41
npm WARN api#1.0.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents#1.2.4 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents#1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
audited 2580 packages in 3.287s
found 1 critical severity vulnerability
run `npm audit fix` to fix them, or `npm audit` for details
---> 5982d518a0e8
Removing intermediate container f544752d8d41
Step 10/12 : RUN node -v
---> Running in a08d0492d376
v10.12.0
---> c41e54a1910c
Removing intermediate container a08d0492d376
Step 11/12 : RUN npm install bcrypt -g --save
---> Running in 874f9d8d7bcf
> bcrypt#3.0.2 install /usr/local/lib/node_modules/bcrypt
> node-pre-gyp install --fallback-to-build
node-pre-gyp WARN Using needle for node-pre-gyp https download
node-pre-gyp WARN Pre-built binaries not installable for bcrypt#3.0.2 and node#10.12.0 (node-v64 ABI, glibc) (falling back to source compile with node-gyp)
node-pre-gyp WARN Hit error EACCES: permission denied, mkdir '/usr/local/lib/node_modules/bcrypt/lib'
gyp WARN EACCES user "undefined" does not have permission to access the dev dir "/root/.node-gyp/10.12.0"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/bcrypt/.node-gyp"
gyp WARN install got an error, rolling back install
gyp WARN install got an error, rolling back install
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/bcrypt/.node-gyp'
gyp ERR! System Linux 4.15.0-42-generic
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/usr/local/lib/node_modules/bcrypt/lib/binding/bcrypt_lib.node" "--module_name=bcrypt_lib" "--module_path=/usr/local/lib/node_modules/bcrypt/lib/binding" "--napi_version=3" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v64"
gyp ERR! cwd /usr/local/lib/node_modules/bcrypt
gyp ERR! node -v v10.12.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/usr/local/lib/node_modules/bcrypt/lib/binding/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/usr/local/lib/node_modules/bcrypt/lib/binding --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v64' (1)
node-pre-gyp ERR! stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/bcrypt/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack at ChildProcess.emit (events.js:182:13)
node-pre-gyp ERR! stack at maybeClose (internal/child_process.js:962:16)
node-pre-gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
node-pre-gyp ERR! System Linux 4.15.0-42-generic
node-pre-gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/bcrypt/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /usr/local/lib/node_modules/bcrypt
node-pre-gyp ERR! node -v v10.12.0
node-pre-gyp ERR! node-pre-gyp -v v0.11.0
node-pre-gyp ERR! not ok
Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/usr/local/lib/node_modules/bcrypt/lib/binding/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/usr/local/lib/node_modules/bcrypt/lib/binding --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v64' (1)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! bcrypt#3.0.2 install: `node-pre-gyp install --fallback-to-build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the bcrypt#3.0.2 install 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/2018-12-07T15_41_19_990Z-debug.log
The command '/bin/sh -c npm install bcrypt -g --save' returned a non-zero code: 1
This is working find on my local ubuntu 18.04, but not on docker.
Any idea what is wrong ?

Resources