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