Docker: Permission error

Try using npm ci rather than npm install… It worked for me…
here is my dockerfile code:

FROM node:16.3.0-alpine3.13      
RUN addgroup app && adduser -S -G app app                                                                                                               
USER app                                                                                                                         
WORKDIR /app
COPY . .
RUN npm ci
ENV API_URL = http://api.myapp.com/
EXPOSE 3000
CMD npm start

Hi,

i posted the solution in this post:

1 Like

Hahahaha the chmod 777 part cracks me up for some reason :joy: :joy: :joy:

My working solution:
(But sometimes i get ERR_SOCKET_TIMEOUT error,if you get the same re-run the docker build. It worked for me.)

FROM node:16.6.2-alpine3.14
RUN addgroup app && adduser -S -G app app
RUN mkdir /app && chown -R app:app /app
USER app
WORKDIR /app
COPY --chown=app:app package*.json ./
RUN npm ci
COPY --chown=app:app . .
ENV API_KEY=secretkey
EXPOSE 3000
CMD ["npm","start"]
1 Like

This looks good actually. Apparently, when you copy files using the COPY instructions, the files get copied with the root as the default user and group. So you have to change the ownership of the files when using the COPY instruction using the --chown option.

You can always adjust the runtime configuration resources (ram, CPU, swap, etc) of containers.

That’s the one, yes!
Permissions have to be set for the right folder and files so that the app user can perform all the tasks like “npm install” …
Thanks @theubsin

As many said before, the COPY command is executed with the ROOT user, so, the app folder is owned by the root.
When copying, we need to change the user to our target user.

FROM node:14.16.0-alpine3.13

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

USER app

WORKDIR /app

COPY --chown=app:app package*.json .

RUN npm install

COPY --chown=app:app . .

ENV API_URL=http://behelp.com/api

EXPOSE 3000

CMD [“npm”, “start”]