Agent Wranglers

INF-73Required

Run containers as a user without root

Switch to a user without root with USER before the app starts. Give that user write access only to the folders the app writes.

How to do it

Many base images ship such a user, for example node in the Node images. Create the data folder and change its owner before the USER line. Drop extra capabilities at run time, for example with cap_drop: [ALL] in Compose. The service sandbox gives the same idea for services without containers.

Examples

Dockerfile example

RUN mkdir -p /data && chown node:node /data
USER node

How to check

docker run --rm shop id -u prints a number other than 0.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/infrastructure/containers-and-orchestration/non-root-containers">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.