Skip to content

Commit

Permalink
Fix permission issue encountered on Docker hub
Browse files Browse the repository at this point in the history
The first builds on docker hub all failed. The error we were getting was

```
[91m ERR! Error: EACCES: permission denied, open '/home/node/package.json'
npm ERR! [Error: EACCES: permission denied, open '/home/node/package.json'] {
npm ERR!�[0m
[91m errno: -13,
npm ERR! code: 'EACCES',
npm �[0m
[91mERR! syscall: 'open',
npm ERR! path: '/home/node/package.json'

...

�[91m 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!�[0m
�[91m
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.
�[0m
�[91m
�[0m
�[91mnpm ERR! A complete log of this run can be found in:
npm ERR!�[0m
�[91m /home/node/.npm/_logs/2021-02-18T09_51_29_644Z-debug.log
�[0m
Removing intermediate container 04acee56b734
The command '/bin/sh -c npm install' returned a non-zero code: 243
```

Some searching of the net indicated that the problem is because the `node` user that is provided in the base image and which we set as the main user does not own the `package.json` files we copy into the image.

It's best practise not to leave the user as `root` in a Docker image, nor to run `npm install` as `root`.

We now understand the reason the `node` user is not the owner of the files is because all `COPY` commands in a Dockerfile are carried out as the `root` user. The suggested fix is to include [chown](https://en.wikipedia.org/wiki/Chown) within your copy statement to ensure once the files are available the non-root user is made the owner.

- nodejs/docker-node#1262 (comment)
- https://stackoverflow.com/a/44766666/6117745
  • Loading branch information
Cruikshanks committed Feb 19, 2021
1 parent ca7ecae commit ac78078
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ EXPOSE $PORT 9229 9230

# Install dependencies first, in a different location for easier app bind mounting for local development. To do this we
# first copy just the package*.json files from the host
COPY package.json package-lock.json* ./
# Note. COPY is always run as the root user in Docker. So, to avoid permission issues we immediately make the node user
# owner of the copied files
COPY --chown=node:node package.json package-lock.json* ./

RUN npm install

Expand Down Expand Up @@ -99,7 +101,9 @@ EXPOSE $PORT

# Install dependencies first, in a different location for easier app bind mounting for local development. To do this we
# first copy just the package*.json files from the host
COPY package.json package-lock.json* ./
# Note. COPY is always run as the root user in Docker. So, to avoid permission issues we immediately make the node user
# owner of the copied files
COPY --chown=node:node package.json package-lock.json* ./

RUN npm install

Expand Down

0 comments on commit ac78078

Please sign in to comment.