How to run container as non-root user?

I’m building an image from the following Dockerfile and following command docker build --rm -f "Dockerfile" -t non_root_image_plz_work .:


FROM node:14.7.0-buster-slim AS apache_for_selenium

# Create non-root group and user
RUN addgroup --system shared-folder 
&& adduser --system --home /var/cache/shared-folder --group shared-folder --uid 1001

# Make Port accessable 
EXPOSE 80/tcp

# Set Node env.Name

RUN apt-get -qq update && apt-get -qq install -y --no-install-recommends nano git openssl bash musl curl apache2 apache2-utils systemd && 
    systemctl enable apache2 && npm config set registry http://localhost:5000/repository/repo && 
    npm i -g pm2 serve && mkdir /usr/share/shared-folder

RUN ln -sf /dev/stdout /var/log/apache2/access.log && 
    ln -sf /dev/stderr /var/log/apache2/error.log    

WORKDIR /usr/share/shared-folder 

COPY . /usr/share/shared-folder/

RUN npm install && npm cache clean --force && npm cache verify && 
    rm /var/www/html/index.html && 
    ln -s /usr/share/shared-folder/mochawesome-report /var/www/html/mochawesome-report && 
    chown www-data -R /var/www/html/mochawesome-report && chgrp www-data -R /var/www/html/mochawesome-report 

VOLUME /usr/share/shared-folder/mochawesome-report

USER 1001

CMD [ "sh", "-c", "service apache2 start ; pm2-runtime process.yml --no-daemon" ]

When I try to run the image using docker run non_root_image_plz_work, I get the following error:

Error after running docker run command:
Starting Apache httpd web server: apache2 failed!
The apache2 configtest failed. … (warning).
Output of config test was:
mkdir: cannot create directory ‘/var/run/apache2’: Permission denied
chown: changing ownership of ‘/var/lock/apache2.3FGoa8Y71E’: Operation not permitted

It seems to be a permissions issue, as if I’m not properly running the container as a non-root user. Any suggestions on how I can get the container to run properly as a non-root user?

Note: I used a dummy registry in the Dockerfile for I don’t want to show the actual registry, thanks

Source: Dockerfile Questions