run-containers-as-a-non-root-user
Use when a container should not have root inside it even if the host is safe. Creates and switches to an unprivileged UID pinned in the image so a process compromise is not a root compromise.
- License 许可
- MIT
- 最近更新
- 2026-11-12
- Stars
- ★ 81
- Forks
- 17
01 前言 Front Matter
- name
- run-containers-as-a-non-root-user
- description
- Use when a container should not have root inside it even if the host is safe. Creates and switches to an unprivileged UID pinned in the image so a process compromise is not a root compromise.
02 资源文件 3
- 许可证LICENSE1.0 KB
- 参考文档SOURCE.md359 B
- 参考文档TRUST.auto.yaml2.2 KB
03 SKILL.md 正文
Run Containers as a Non-Root User
By default a container runs as UID 0, and without a user namespace that is host root for many escape and volume paths. The fix is a numeric, explicit UID baked into the image plus a runtime refusal to run as root.
Procedure
- Create the user in the image with a fixed numeric UID (names are resolved in the image, so a bare
USER appfails on a base that lacks the passwd entry):RUN groupadd -g 10001 app && useradd -u 10001 -g 10001 -m -s /sbin/nologin app USER 10001:10001 chownthe working dir and any writable paths before switching, since root-only writes afterUSERfail:RUN chown -R 10001:10001 /app && chmod -R u+rwX /app.- At runtime, make the intent explicit and defensive:
docker run --user 10001:10001 --read-only --tmpfs /tmp app:x. - In Kubernetes set
runAsNonRoot: trueand a numericrunAsUser, which the kubelet refuses to violate:securityContext: runAsNonRoot: true runAsUser: 10001 allowPrivilegeEscalation: false capabilities: { drop: ["ALL"] } - Enforce cluster-wide with the Pod Security Standard
restrictedprofile, or a Kyverno rule requiringrunAsNonRoot: trueon every pod. - Check what a process can do:
docker run --rm app:x idmust printuid=10001. For ports, bind to>1024— non-root cannot bind80/443, so publish with-p 8080:8080and let the proxy handle the low port. - If a supervisor needs a low port, do not grant
CAP_NET_BIND_SERVICEbroadly; drop ALL and add back only that one capability.
Pitfalls
- Setting
USERin the Dockerfile does not stopdocker run --user 0; onlyrunAsNonRoot: truein the orchestrator enforces it. - A base image's
USER 1000is often overridden by a laterRUNthat escalates toUSER rootand never switches back — the image ships as root and nobody notices. chown -Rin a huge layer doubles image size; prefer creating files as the target user or--chown=10001:10001onCOPY.- Volume mounts from the host arrive owned by the host's UID; a container non-root user then cannot write them. Set
fsGroup(K8s) or--usermatching the host, or use an init container to chown. - Random UIDs from
runAsUserranges break images whose entrypoint reads/etc/passwdfor a home directory; mount a writableHOMEor an emptyDir instead. allowPrivilegeEscalation: falsestill permits a setuid binary already in the image — remove setuid bits in the Dockerfile (RUN find / -perm -4000 -exec chmod u-s {} +).
Verification
docker run --rm app:x id
# uid=10001(app) gid=10001(app) groups=10001(app)
The command prints a non-zero UID and the container runs its healthcheck without permission errors. Report the observed UID/GID and the policy that enforced it.
04 同仓库更多 Skills
recipe-performance
Diagnose a specific MetaMask user flow's real-app performance and Sentry critical-user-flow telemetry with deterministic mm-harness recipes. Use for CUF boundaries, cold/warm/background-resume behavior, latency regressions, lifecycle tags, duplicate or blocking network requests, long tasks, layout/paint cost, screen redraws, React rerenders, or browser trace analysis where tests alone cannot prove the real app.
mexc-spot-websocket
MEXC Spot WebSocket workflows for public streams, protobuf handling, depth maintenance, listen keys, and private streams without exposing API keys.
mexc-spot-rest
MEXC Spot REST workflows for public data, signed account requests, orders, wallet/admin actions, and secure signing without exposing API keys.
mexc-futures-websocket
MEXC Futures WebSocket workflows for public streams, private account/order/position streams, ping/pong, subscriptions, and depth maintenance.
0xklen-version-and-refresh-sanctions-lists
暂无简介
verify-tests-cover-changed-lines
Use when a PR adds code that the existing suite may not exercise. Measures patch coverage — the share of newly changed lines hit by tests — before approving.