Permission issues in my Dockerfile

If I made root the “user” in the react-app the image will build. But when I made “App” the user the build will fail.

See my Dockerfile below:

FROM node:14.21.2-alpine3.17

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

USER root

WORKDIR /app

COPY package*.json .

RUN npm install && npm audit fix --force

COPY . .

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

EXPOSE 3000

ENTRYPOINT [ “npm”, “start” ]

Anyone with suggestion?

Thank you.

wakandom

Here’s how I managed to fix the permission issue.

FROM node:20.10.0-alpine3.19

# React app lives here
WORKDIR /app

# Set environment variables
ENV TEST="Hello Docker!"

# Install application packages
COPY package*.json .
RUN npm install

# Copy application code
COPY . .

# Add a non-root user
RUN addgroup app && \
    adduser -S -G app app

# Run and own only the runtime files as a non-root user for security
RUN mkdir node_modules/.cache && \
    chown app:app node_modules/.cache

# Switch to non-root user
USER app

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["npm", "start"]