Anh Thang Bui

07 Jul 2018 ~ 1 min read

Running a Docker container as non-root user


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~


Anh Thang Bui

Hi, I'm Thang.
I'm a software engineer from Hanoi, Vietnam. You can follow me on Twitter, see some of my work on GitHub, or read more about me on my website.