Sub topic 8
Dropping root privileges
This chapter assumes you can write a basic Dockerfile, build an image, and run a container with published ports. By default, processes in a container run as root. This chapter shows why that is a risk and how to run your Node.js app as an unprivileged user.
Why root in a container is a problem
A container shares the host's kernel. The root user inside a container is not the same as an administrator on the host, but it is more powerful than your app needs. If an attacker exploits a bug in your app, they get root inside the container. From there they can install tools, change files in the image, and try to escape the container through a kernel or runtime flaw.
Running as a non-root user limits the damage. The app can no longer write to system directories, install packages, or bind to ports below 1024. This is the principle of least privilege: give a process only the access it needs.
The USER instruction
The USER instruction sets which user runs all later instructions and the container's main process. The official node images already include a user named node (UID 1000), so you only need to switch to it.
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
USER node
CMD ["node", "server.js"]Place USER node after the steps that need root, such as installing dependencies, and before CMD. Instructions after USER also run as node, so a later RUN npm install -g something would fail.
Verifying the user
Build and run the image, then check who the process runs as.
docker build -t myapp:1.0 .
docker run --rm -d -p 3000:3000 --name myapp myapp:1.0
docker exec myapp whoami
docker exec myapp id
docker stop myappwhoami should print node. If it prints root, the USER line is missing or comes before a later stage that resets it.
Common errors
Permission denied when writing files
The COPY instruction creates files owned by root by default. If your app writes to its own directory, for example a cache or log file, it fails with EACCES. Use --chown to give the files to the right user.
COPY --chown=node:node . .Alternatively, create a dedicated writable directory and set its owner:
RUN mkdir /app/tmp && chown node:node /app/tmpPorts below 1024
Unprivileged users cannot bind to ports such as 80 or 443. Keep the app on a high port like 3000 inside the container. Map it to port 80 on the host with -p 80:3000 if you need to.
Custom users in other images
Not every base image includes a ready-made user. Images based on Debian or Alpine without one need you to create it.
FROM alpine:3.20
RUN addgroup -S app && adduser -S -G app app
USER appOn Debian-based images, use groupadd and useradd instead.
How the pieces fit together
The diagram shows the order of operations in the Dockerfile and where the privilege change happens.
Summary of choices
| Approach | When to use it |
|---|---|
| `USER node` | Official Node.js images, which already include the user |
| `addgroup` and `adduser` | Alpine images without a ready-made user |
| `COPY --chown` | Files that the app must modify at run time |
Exercise
Your app starts as the node user and crashes with EACCES: permission denied when it tries to create /app/cache. The COPY . . step ran before USER node. What are two ways to fix it?
Show answer
Files copied by COPY are owned by root, so node cannot write to /app. First, use COPY --chown=node:node . . so the files belong to node. Second, create the directory before switching users with RUN mkdir /app/cache && chown node:node /app/cache. Either way, the writable path must be owned by node.
Exercise
After building your image, which command shows that the running container is not using root, and what output do you expect?
Show answer
Run docker exec myapp whoami against the running container. It should print node. docker exec myapp id also works and shows a UID of 1000.