How to configure Dockerfile to run container as non-root user

Hello there, I would like to build image to run as non-root user, getting error while running docker run “”/var/cache/nginx/client_temp" failed (13: Permission denied)"

this is my Dockerfile

t# base image
FROM node:18-alpine as builder

# build-time variables
ARG environment=production

# install Git in the Alpine Linux image
RUN apk add --no-cache git

# set working directory
WORKDIR /webapp

# install the package manager pnpm globally
RUN npm install -g pnpm
COPY . .

# install dependencies
# RUN pnpm install --frozen-lockfile
RUN pnpm install --no-frozen-lockfile
ENV APP_ENV=$environment

# generate build
RUN pnpm nx:build

# base image
FROM nginx:stable-alpine as runtime

# copy the Nginx configuration file
COPY ./nginx/nginx.conf /etc/nginx/nginx.conf

# copy artifact build from the 'build environment'
COPY --from=builder /webapp/apps/admin-portal-frontend/dist /usr/share/nginx/html

# run container as app user instead of root user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser 

# expose port
EXPOSE 80
# run nginx
CMD ["nginx", "-g", "daemon off;"]

Thanks in advance for your suggestion

The most obvious suggestion is to take a look at the image description on Docker Hub. The section Running nginx as a non-root user contains the relevant information.

1 Like