# Docker: npm permission denied

**URL:** <https://forum.codewithmosh.com/t/docker-npm-permission-denied/4766>\
**Category:** Uncategorized\
**Created:** [May 8, 2021, 3:35pm UTC](https://forum.codewithmosh.com/t/docker-npm-permission-denied/4766 "2021-05-08T15:35:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yashrathi](https://avatars.discourse-cdn.com/v4/letter/y/ee7513/32.png) [@yashrathi](https://forum.codewithmosh.com/u/yashrathi)\
**Post date:** [May 8, 2021, 3:35pm UTC](https://forum.codewithmosh.com/t/docker-npm-permission-denied/4766/1 "2021-05-08T15:35:15Z")

</div>

Iam struggling with permission error in using npm in Section 4: Building images  
My problem is similar to:

> [@Docker: Permission error](https://forum.codewithmosh.com/t/docker-permission-error/3818):
>
> I’m having some trouble with “Setting the User” in the “Building Images” section in the Docker course. My local machine is running Ubuntu and when I run this command: sudo docker build -t react-app . I get this error from npm: Sending build context to Docker daemon 2.248MB Step 1/9 : FROM node:14.16.0-alpine3.13 —\> 50bfd284aa0d Step 2/9 : RUN addgroup app && adduser -S -G app app —\> Using cache —\> d6571e5eb8c6 Step 3/9 : USER app —\> Using cache —\> 21377574fca0 Step 4/9 : WORKDIR /ap…

However I can’t find the solution there, that’s why I am creating a new topic  
When I follow the instructor still I get this permission error:

Dockerfile

```auto
FROM node:14.16.0-alpine3.13
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app
COPY . .
RUN npm install
ENV API_URL=http://api.myapp.com/
EXPOSE 3000
CMD [“npm”, “start”]

```

```auto
Step 6/9 : RUN npm install
—> Running in 103nj60a010c2
npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@2. I’ll try to do my best with it!
npm WARN checkPermissions Missing write access to /app
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /app
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access ‘/app’
npm ERR! [Error: EACCES: permission denied, access ‘/app’] {
npm ERR! errno: -13,
npm ERR! code: ‘EACCES’,
npm ERR! syscall: ‘access’,
npm ERR! path: ‘/app’
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! /home/app/.npm/_logs/2021-04-02T22_17_49_152Z-debug.log

```

Then from the above this [thread](https://forum.codewithmosh.com/t/docker-permission-error/3818) I updated my Docker file.

My new dockerfile contents:

```lang-dockerfile
FROM node:16.0.0-alpine3.13
RUN addgroup app && adduser -S -G app app
RUN mkdir /app && chown app:app /app
USER app

WORKDIR /app
COPY . .
RUN npm install

```

When I try to build the container I get this permission error:

```sh
npm ERR! code EACCES
npm ERR! syscall open
npm ERR! path /app/package-lock.json
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, open '/app/package-lock.json'
npm ERR! [Error: EACCES: permission denied, open '/app/package-lock.json'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'open',
npm ERR! path: '/app/package-lock.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! /home/app/.npm/_logs/2021-05-08T12_59_30_045Z-debug.log

```

I don’t understand, why I am getting this error as the parent directory is owned by the `app` user, and it also have permissions to read `package.lock` and `package.json` files.

If I remove the `RUN npm install` from Dockerfile and build the container, this is the output of `ls -l`

```auto
-rw-r--r-- 1 root root 660074 May 5 12:33 package-lock.json
-rw-r--r-- 1 root root 813 Mar 5 19:00 package.json

```

The other user have read permission. And that seems to be the permissions  
`npm install` needs.  
Why am I getting permission error here? Please help

---

<div class="post-metadata">

**Author:** ![Osama210690](https://avatars.discourse-cdn.com/v4/letter/o/bbe5ce/32.png) [@Osama210690](https://forum.codewithmosh.com/u/Osama210690)\
**Post date:** [September 26, 2021, 6:59am UTC](https://forum.codewithmosh.com/t/docker-npm-permission-denied/4766/3 "2021-09-26T06:59:01Z")

</div>

I researched and tested for 6 Hours and found that the below SCRIPT 1 as shown in Mosh’s lecture video is working only if you use the same version of node in video.

# SCRIPT 1 - From Video

FROM node:14.16.0-alpine3.13

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

WORKDIR /app

RUN npm install  
EXPOSE 3000

CMD [“npm”, “start”]

# SCRIPT 2

Latest and other alpine versions have default use NODE which is overriding our app users so below is the solution to use both NODE user and our app users.

###Using custom user i.e. app###

FROM node:16.10.0-alpine3.14

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

USER app

WORKDIR /app

COPY --chown=app:node package\*.json ./

RUN npm install

COPY --chown=app:node . .

EXPOSE 3000

CMD [“npm”, “start”]

###Using default user i.e. node###

FROM node:16.10.0-alpine3.14

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

USER app

WORKDIR /app

COPY --chown=node:node package\*.json ./

RUN npm install

COPY --chown=node:node . .

EXPOSE 3000

CMD [“npm”, “start”]

---

<div class="post-metadata">

**Author:** ![spot](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/spot/32/2329_2.png) [@spot](https://forum.codewithmosh.com/u/spot)\
**Post date:** [September 28, 2021, 12:24am UTC](https://forum.codewithmosh.com/t/docker-npm-permission-denied/4766/4 "2021-09-28T00:24:23Z")

</div>

Thank you! I was pulling my hair out… and I’m bald.

---

<div class="post-metadata">

**Author:** ![shaunumb](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/shaunumb/32/3299_2.png) [@shaunumb](https://forum.codewithmosh.com/u/shaunumb)\
**Post date:** [February 12, 2022, 8:25am UTC](https://forum.codewithmosh.com/t/docker-npm-permission-denied/4766/5 "2022-02-12T08:25:23Z")

</div>

Hi

I was going to create a post with my solution as I encountered the same problem. The specific error was with package-lock.json so forcing the user and group of package\*.json files to be ‘app’ seems to also work.

```auto
FROM node:16.14.0-alpine3.15
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app
COPY --chown=app:app package*.json .
RUN npm install
COPY . .
ENV API_URL=http://api.blah.com
EXPOSE 3000

CMD ["npm", "start"]

```

---

<div class="post-metadata">

**Author:** ![herrifransisca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/herrifransisca/32/953_2.png) [@herrifransisca](https://forum.codewithmosh.com/u/herrifransisca)\
**Post date:** [May 27, 2023, 4:51am UTC](https://forum.codewithmosh.com/t/docker-npm-permission-denied/4766/6 "2023-05-27T04:51:13Z")

</div>

Hi,

Thanks @Osama210690 and @shaunumb

I’ve tested and is working.

I checked Dockerfile command layer per layer and finally the “content of Dockerfile” is exactly the same with @shaunumb.

Here’s my Dockerfile:  
Note: I’m using the latest node alpine version

```auto
FROM node:alpine 

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

WORKDIR /app 
COPY --chown=app:app package*.json ./
RUN npm install 
COPY . .

EXPOSE 5173 

CMD ["npm", "run", "dev"]

```

---

<div class="post-metadata">

**Author:** ![khraja](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@khraja](https://forum.codewithmosh.com/u/khraja)\
**Post date:** [July 12, 2023, 2:35pm UTC](https://forum.codewithmosh.com/t/docker-npm-permission-denied/4766/7 "2023-07-12T14:35:46Z")

</div>

Hi everybody,

I have the same issue with my Docker compose. I have a MERN Stack Application. When I Build my project with Docker compose on Windows it works fine but on Linux (Ubuntu) I got the following error in my React App container.  
`sh: react-scripts: permission denied`

Here is my Project Structure  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/6/6e05bb8aaf76988b9892a1b979808414edf2754b.png)

My Docker file in react-app

```auto
FROM node:16.14.0-alpine3.15
ENV NODE_ENV=production
WORKDIR /usr/src/react-app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm install
COPY . .
EXPOSE 3000
RUN chown -R node /usr/src/react-app
USER node
CMD ["npm", "start"]

```

Here is my script from package.json in react-app

```auto
"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "lint": "eslint --fix"
  },

```

I also tried the solution from @shaunumb but unfortunatily it does’nt work for me.  
Any suggestions ?
