Query about adding user in dockerfile

Hello there,
sir, I am stuck on a topic. when I add the user in Dockerfile at the end and building an image from it and after that when I start a container from this image by using the following command
docker run react-app npm start
it gives the permission denied error.
because you an I created build as a root user and defined the new app user at the end. when I following you moved the new user at the top then the permission denied error becomes while building the image. kindly help to resolve this problem. the content of Dockerfile before and after is attaching with this post

Before

FROM node:14.16.1-alpine3.13
WORKDIR /app/
COPY package*.json .
RUN npm install
COPY . .
ENV API_URL=https://api.myapp.com
EXPOSE 3000
RUN addgroup app && adduser -S -G app app
USER app

creating image from it
docker build -t react-app

error after running the command ā€œdocker run react-app npm startā€

"
:information_source: ļ½¢wdsļ½£: Project is running at http://172.17.0.2/
:information_source: ļ½¢wdsļ½£: webpack output is served from
:information_source: ļ½¢wdsļ½£: Content not from webpack is served from /app/public
:information_source: ļ½¢wdsļ½£: 404s will fallback to /
Starting the development serverā€¦
Failed to compile.
EACCES: permission denied, mkdir ā€˜/app/node_modules/.cacheā€™

"

After
Dockerfile

FROM node:14.16.1-alpine3.13
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app/
COPY package*.json .
RUN npm install
COPY . .
ENV API_URL=https://api.myapp.com
EXPOSE 3000

error while building the image after running command ā€œdocker build -t react-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! }

"

Go with the default user (node). No need to declare explicitly

  • I tried custom user ā€œappā€ but to no success. Same issue will arise in later stages.

    FROM node:14.16.0-alpine3.13

    RUN mkdir app

    WORKDIR /app

    COPY package*.json ./

    RUN npm install

    COPY . .

    EXPOSE 3001

    CMD [ā€œnpmā€, ā€œstartā€]

1 Like