For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/default_rules/qbp-5k8-mec.md. A documentation index is available at /llms.txt.

Containers should run as a non-root user

docker

Classification:

compliance

Framework:

cis-docker

Control:

4.1

Description

Containers should run as a user other than root.

Rationale

A process that escapes a container keeps its user. If it runs as an unprivileged user, it lands on the host without root privileges.

Audit

Show the user of the running containers:

docker ps --quiet | xargs docker inspect --format '{{ .Name }}: User={{ .Config.User }}'

Datadog checks the running containers. It passes the rule when the container’s user, before any :, is set and is other than root and the UID 0, in any spelling that Docker reads as 0, such as 00, and fails it otherwise. A container that drops privileges in its entrypoint fails, and needs a manual review.

Remediation

Add a user to the image and switch to it with a USER instruction in the Dockerfile:

RUN useradd -r -u 10001 app
USER app

Or run the container with --user.

Impact

Programs that need root inside the container stop working.

Default value

Containers run as root unless the image or the command sets a user.

References

  1. https://docs.docker.com/reference/dockerfile/#user
  2. https://docs.docker.com/engine/containers/run/#user