zuzuzuu
zuzuzuu

Reputation: 87

Docker Permission error while not using root

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 [email protected]` 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

Upvotes: 0

Views: 372

Answers (1)

David Maze
David Maze

Reputation: 158647

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.

Upvotes: 2

Related Questions