Running a Docker container as non-root user

July 07, 2018

By default, Docker container will be run as root user, this can cause us pain. We could tell Docker to run as an ordinary user instead of root.

By default, Docker container will be run as root user, this can cause us pain. We could tell Docker to run as an ordinary user instead of root.

We could build a Dockerfile like this:

FROM node:latest

# Add user so we dont run app as root
RUN groupadd -r node && useradd -r -g node node \
    && mkdir -p /home/node/<your-app-name> \
    && chown -R node:node /home/node

WORKDIR /home/node/<your-app-name>

COPY . .
RUN npm install

USER node
EXPOSE 3000

CMD ["npm", "start"]

Happy building v


Profile picture

Written by Anh Thang who lives and works in Hanoi. You can find me on Twitter or Github