8
FROM node:14.16.0-alpine3.13
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package*.json ./
RUN npm install
COPY . .
ENV APP_URL=http://api.myapp.com
EXPOSE 3000
CMD ["npm", "start"]

This is my docker file. I am trying to dockerize a sample react-app. I added user in group and then using that user for further commands as you can see this in second line of this code. I believe by default, only root user has access to write to these files and in order to do changes in these files, root user should not be user. Hence I created app user here.

But after running docker build -t react-app.. I am getting the following error -

enter image description here

What am I doing wrong here? Any suggestions?

After adding Run ls -la and Run whoami - enter image description here

7
  • I didn't work. But why is it throwing this error? Commented Apr 7, 2021 at 6:45
  • In your Dockerfile, can you add RUN ls -la and RUN whoami after COPY package*.json ./ and let us know what the output is? Commented Apr 7, 2021 at 7:00
  • 1
    I might do all of the package installation as the root user, as @AjeetShah suggests, and switch to USER app only at the very very end where you specify the runtime CMD as well. This will let the installation run, but prevent the application from modifying its own source or assets. Commented Apr 7, 2021 at 10:22
  • Can you change this line COPY package.json ./ ? package-lock.json might cause problem. Commented Apr 7, 2021 at 10:52
  • @AjeetShah Even if I use root user, when I start my application using npm start, permissions denied error is again thrown for which I came upon on this solution that we should not use root user. Plus it is not recommended as well. Commented Apr 7, 2021 at 11:16

2 Answers 2

7

You are seeing this error because /app directory belongs to root user. The user app which you created has no write permission to this directory. User app needs write permission to install node packages (create node_modules directory and package-lock.json file).

As suggested by @DavidMaze in comments, it would be easy to do package installation as root user and switch to USER app at the last but before the runtime CMD ["npm", "start"].

But still app user would need write permission to node_modules/.cache directory when running the app with npm start command. Hence, we need to provide the write permission to user app for this directory.

Here is an example that does all mentioned above:

FROM node:14.16.0-alpine3.13
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package*.json ./
RUN npm install
COPY . .
ENV APP_URL=http://api.myapp.com
EXPOSE 3000

RUN addgroup app && adduser -S -G app app
RUN mkdir node_modules/.cache
RUN chown app:app node_modules/.cache

USER app
CMD ["npm", "start"]

Also, note that you are running the React App in development mode using npm start, you might want to use a static server to serve, after creating a build.

Sign up to request clarification or add additional context in comments.

4 Comments

You should also keep a .dockerignore; inside which node_modules and other appropriate files or directories should be added.
Yes. I do have a .dockerignore file which keeps all the appropriate files.
Hello @AjeetShah I'm having a similar problem if you can help. This is part of my dockerfile and I use to run cypress tests inside the bash but after applying this fix I always get error No version of Cypress is installed in: /home/node/.cache/Cypress/8.7.0/Cypress USER node WORKDIR /app COPY package*.json ./ RUN npm install COPY ./cypress.json ./cypress.json COPY --chown=node:node . . CMD [ "bash" ]
@Mona101ma This is very old post. Can you help me reproduce this error by giving some minimal code? But it seems like you need to have cypress installed in your docker.
0

when you create user with name "app", a new directory for this user is created in home directory "/home/app" so,workdir should be like this:

    WORKDIR /home/app

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.