Docker Permission error while not using root - docker

When i create dockerfile using this way I'll run into error
FROM node:18.12.1-alpine3.17
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app
COPY package*.json .
COPY . .
RUN npm install
ENV API_URL=http://api.myapp.com/
EXPOSE 3000
CMD ["npm", "start"]
This is the error i get...
C:\Users\user\Desktop\react-docker>docker build -t react-app .
[+] Building 17.5s (10/10) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 264B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 34B 0.0s
=> [internal] load metadata for docker.io/library/node:18.12.1-alpine3.17 0.5s
=> [internal] load build context 0.0s
=> => transferring context: 4.74kB 0.0s
=> [1/6] FROM docker.io/library/node:18.12.1 - 0.0s
=> CACHED [2/6] RUN addgroup app && adduser -S -G app app 0.0s
=> CACHED [3/6] WORKDIR /app 0.0s
=> [4/6] COPY package*.json . 0.1s
=> [5/6] COPY . . 0.0s
=> ERROR [6/6] RUN npm install 16.8s
------
> [6/6] RUN npm install:
#10 13.03 npm notice
#10 13.03 npm notice New major version of npm available! 8.19.2 -> 9.4.2
#10 13.03 npm notice Changelog: <https://github.com/npm/cli/releases/tag/v9.4.2>
#10 13.03 npm notice Run `npm install -g npm#9.4.2` to update!
#10 13.03 npm notice
#10 13.03 npm ERR! code EACCES
#10 13.03 npm ERR! syscall open
#10 13.03 npm ERR! path /app/package-lock.json
#10 13.03 npm ERR! errno -13
#10 13.03 npm ERR! Error: EACCES: permission denied, open '/app/package-lock.json'
#10 13.03 npm ERR! [Error: EACCES: permission denied, open '/app/package-lock.json'] {
#10 13.03 npm ERR! errno: -13,
#10 13.03 npm ERR! code: 'EACCES',
#10 13.03 npm ERR! syscall: 'open',
#10 13.03 npm ERR! path: '/app/package-lock.json'
#10 13.03 npm ERR! }
#10 13.03 npm ERR!
#10 13.03 npm ERR! The operation was rejected by your operating system.
#10 13.03 npm ERR! It is likely you do not have the permissions to access this file as the current user
#10 13.03 npm ERR!
#10 13.03 npm ERR! If you believe this might be a permissions issue, please double-check the
#10 13.03 npm ERR! permissions of the file and its containing directories, or try running
#10 13.03 npm ERR! the command again as root/Administrator.
#10 13.03
#10 13.03 npm ERR! A complete log of this run can be found in:
#10 13.03 npm ERR! /home/app/.npm/_logs/2023-02-10T01_12_33_542Z-debug-0.log
------
executor failed running [/bin/sh -c npm install]: exit code: 243
Already saw other person like him: https://forum.codewithmosh.com/t/permission-issues-in-my-dockerfile/17216
running into same error but unfortunately no suggestion how to fix it

I'd do two things here.
The first is to run npm ci instead of npm install. This will install the exact set of things listed in the package-lock.json, and never try to modify it. That should avoid the problem where npm can't write the lock file.
The second change is to move the USER app line to the end of the Dockerfile. Run the entire build sequence as root. Your application, its source code, and the node_modules directory will all be owned by root; but they will also all be world-readable. Your build will run successfully, but when you start the container, it won't have write permission to alter its own source code, libraries, or static assets. That's generally a more secure setup and won't usually impact your application at all.

Related

How to aim a package.json file in dockerfile if the dockerfile is inside another folder in the root directory

Currently the structure of the directory is
root/
package.json
app.js
docker/
backend.Dockerfile
docker-compose.yaml
and when i try to run the docker compose it keeps failing and says
> [4/4] RUN npm install:
#8 0.564 npm ERR! code ENOENT
#8 0.565 npm ERR! syscall open
#8 0.565 npm ERR! path /usr/src/app/package.json
#8 0.565 npm ERR! errno -2
#8 0.566 npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
#8 0.566 npm ERR! enoent This is related to npm not being able to find a file.
#8 0.566 npm ERR! enoent
#8 0.567
#8 0.567 npm ERR! A complete log of this run can be found in:
#8 0.567 npm ERR! /root/.npm/_logs/2022-12-27T09_15_42_645Z-debug-0.log
i understand the error means it couldnt find the package.json but on my dockerfile i already assign to copy the right directory here is my dockerfile
FROM node:18
WORKDIR /app
COPY ../ .
RUN npm install
EXPOSE 8000
CMD ["npm", "run", "dev"]
and here is my docker compose file
version: '3'
services:
app:
build:
context: .
dockerfile: backend.Dockerfile
volumes:
- ../:/app:cached
ports:
- "8000:8000"
Im not sure on where i did wrong but im pretty sure its my path's problem can anyone help me
So what i have try is that changing the path on the COPY command in the docker file but it seems to not work but if i move my dockerfile to the root directory it work coz my cmd in the dockerfile was
COPY . .
but when i move it into a folder inside the root directory so the structure would be root/docker/dockerfile
i decided to change the COPY command into COPY ../ ./ but it doesnt work and i got that error called
> [4/4] RUN npm install:
#8 0.564 npm ERR! code ENOENT
#8 0.565 npm ERR! syscall open
#8 0.565 npm ERR! path /usr/src/app/package.json
#8 0.565 npm ERR! errno -2
#8 0.566 npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
#8 0.566 npm ERR! enoent This is related to npm not being able to find a file.
#8 0.566 npm ERR! enoent
#8 0.567
#8 0.567 npm ERR! A complete log of this run can be found in:
#8 0.567 npm ERR! /root/.npm/_logs/2022-12-27T09_15_42_645Z-debug-0.log
try copying what you have in the root directory
WORKDIR /app
COPY root/. .
RUN npm install

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?

How do I install a node module in a Cypress Docker container?

In the path ~/cypress-dockerfile I have the following Dockerfile:
FROM cypress/base:10
RUN npm install --save-dev cypress
WORKDIR /node_modules
RUN npm install sqlite3
RUN $(npm bin)/cypress verify
RUN $(npm bin)/cypress run
I then run docker build . but it stops with the following:
Step 4/6 : RUN npm install sqlite3
---> Running in 13de6b7a4fcf
npm WARN deprecated request#2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
> sqlite3#5.0.0 install /node_modules/sqlite3
> node-pre-gyp install --fallback-to-build
sh: 1: node-pre-gyp: not found
npm WARN enoent ENOENT: no such file or directory, open '/package.json'
npm WARN !invalid#1 No description
npm WARN !invalid#1 No repository field.
npm WARN !invalid#1 No README data
npm WARN !invalid#1 No license field.
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! sqlite3#5.0.0 install: `node-pre-gyp install --fallback-to-build`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the sqlite3#5.0.0 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/2020-08-07T13_28_54_118Z-debug.log
The command '/bin/sh -c npm install sqlite3' returned a non-zero code: 1
I think that I may be in the wrong directory but I am not sure, any help would be appreciated.

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.

Electron inside Docker with X11 Forwarding - No Protocol Specified

I am attempting to run the Electron Quick Start in a Docker container with X11 forwarding. I've got all the appropriate packages figured out but when I run the container I get No protocol specified. I'm not sure what I'm missing to get it to work.
Dockerfile
FROM node
RUN apt-get update
RUN apt-get -y install libgtkextra-dev libgconf2-dev libnss3 libasound2 libxtst-dev libxss1 libx11-xcb-dev
WORKDIR /srv
ADD . .
RUN npm install
ENTRYPOINT ["npm", "start"]
Build and Run
docker build -t electron .
docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY electron
Output
npm info it worked if it ends with ok
npm info using npm#4.1.2
npm info using node#v7.7.2
npm info lifecycle electron-quick-start#1.0.0~prestart: electron-quick-start#1.0.0
npm info lifecycle electron-quick-start#1.0.0~start: electron-quick-start#1.0.0
> electron-quick-start#1.0.0 start /srv
> electron .
No protocol specified
npm info lifecycle electron-quick-start#1.0.0~start: Failed to exec start script
npm ERR! Linux 4.8.0-41-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v7.7.2
npm ERR! npm v4.1.2
npm ERR! code ELIFECYCLE
npm ERR! electron-quick-start#1.0.0 start: `electron .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the electron-quick-start#1.0.0 start script 'electron .'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the electron-quick-start package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! electron .
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs electron-quick-start
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls electron-quick-start
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /srv/npm-debug.log
Q: What is the correct way of running Electron apps inside Docker?
In your host machine run xhost local:root then try running your container to run electron

Resources