skip to Main Content

I’m new to docker and I’m trying to run npm install as a system user but I can’t do that because this error occured every time and I don’t know what to do.

This is the error massage I got:

38.52 npm ERR! code EACCES
38.52 npm ERR! syscall open
38.52 npm ERR! path /app/package-lock.json
38.52 npm ERR! errno -13
38.52 npm ERR! Error: EACCES: permission denied, open '/app/package-lock.json'
38.52 npm ERR!  [Error: EACCES: permission denied, open '/app/package-lock.json'] {
38.52 npm ERR!   errno: -13,
38.52 npm ERR!   code: 'EACCES',
38.52 npm ERR!   syscall: 'open',
38.52 npm ERR!   path: '/app/package-lock.json'
38.52 npm ERR! }
38.52 npm ERR! 
38.52 npm ERR! The operation was rejected by your operating system.
38.52 npm ERR! It is likely you do not have the permissions to access this file as the current user
38.52 npm ERR! 
38.52 npm ERR! If you believe this might be a permissions issue, please double-check the
38.52 npm ERR! permissions of the file and its containing directories, or try running
38.52 npm ERR! the command again as root/Administrator.
38.52 
38.52 npm ERR! A complete log of this run can be found in: /home/app/.npm/_logs/2024-04-05T19_43_08_160Z-debug-0.log
------
Dockerfile:8
--------------------
   6 |     COPY . .
   7 |     
   8 | >>> RUN npm install
   9 |     
  10 |     EXPOSE 3000
--------------------
ERROR: failed to solve: process "/bin/sh -c npm install" did not complete successfully: exit code: 243

Here is my docker file:

FROM node:20.12-alpine3.18

RUN addgroup app && adduser -S -G app app
USER app

WORKDIR /app

COPY . .

RUN npm install

EXPOSE 3000

Is my dockerfile is correct and how i solve this error?

2

Answers


  1. Possibly this problem arises because, as part of your Dockerfile, you switch to a non-root user (app), but the ownership of the files copied into the container might still be under the root user

    Try to modify your Dockerfile to include a chown step after copying the files:

    FROM node:20.12-alpine3.18
    
    RUN addgroup app && adduser -S -G app app
    
    # Set the working directory in the container
    WORKDIR /app
    
    # Copy the application files and change ownership to the 'app' user
    COPY --chown=app:app . .
    
    USER app
    
    RUN npm install
    
    EXPOSE 3000
    
    Login or Signup to reply.
  2. To address this issue, adjust your Dockerfile so that the user within the container possesses the required permissions to write to the /app directory and execute npm install without encountering permission conflicts.

    FROM node:20.12-alpine3.18
    
    # Create app directory
    WORKDIR /app
    
    # Create a non-root user
    RUN addgroup -g 1001 -S app && 
        adduser -u 1001 -S app -G app
    
    # Change ownership of the working directory to the non-root user
    RUN chown -R app:app /app
    
    # Switch to the non-root user
    USER app
    
    # Copy package.json and package-lock.json first to leverage Docker cache
    COPY package*.json ./
    
    # Install app dependencies
    RUN npm install
    
    # Copy the rest of the application
    COPY . .
    
    EXPOSE 3000
    
    CMD ["npm", "start"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search