---
title: "Understanding the Docker USER Instruction"
description: "Docker containers run as root (UID 0) unless a Dockerfile sets the USER instruction, which widens the damage if a container is compromised."
url: "https://swe.quest/articles/understanding-the-docker-user-instruction-9ebaa799d77dfd97"
canonical: "https://www.docker.com/blog/understanding-the-docker-user-instruction/"
source: "Docker"
published: "2024-06-26"
difficulty: "medium"
kind: "How-to"
reading_minutes: 9
tags: ["containers","security","linux"]
---

# Understanding the Docker USER Instruction

A post on [Docker](https://swe.quest/sources/docker.md), published 2024-06-26, 9 min read, How-to. Original: [https://www.docker.com/blog/understanding-the-docker-user-instruction/](https://www.docker.com/blog/understanding-the-docker-user-instruction/)

## Summary

Docker containers run as root (UID 0) unless a Dockerfile sets the USER instruction, which widens the damage if a container is compromised. Recommended practices: always set USER explicitly, prefer numeric UID:GID over names, and create a dedicated application user with useradd. Switch to root only for privileged build steps such as package installs, and pair USER with WORKDIR and chown so the user can write to its directories and mounted volumes. A side-by-side Dockerfile demo shows the default root user versus a custom UID/GID 1234 user, and user namespaces (used by Docker's Enhanced Container Isolation) can map in-container root to an unprivileged host ID.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Containers](https://swe.quest/tags/containers.md) (Infrastructure)
- [Security](https://swe.quest/tags/security.md) (Security)
- [Linux](https://swe.quest/tags/linux.md) (Infrastructure)

## Prerequisites

- Dockerfile basics
- Linux users and permissions ([easy posts on Linux](https://swe.quest/tags/linux.md?difficulty=easy))

## Keywords

Docker, Dockerfile, USER instruction, Docker Desktop, Enhanced Container Isolation, user namespaces, Docker Scout

## Related posts

- [Kubernetes v1.37: KubeletInUserNamespace (aka Rootless mode) Graduates to Beta](https://swe.quest/articles/kubernetes-v1-37-kubeletinusernamespace-aka-rootless-mode-graduates-to-beta-6c430271478257a8.md): Kubernetes Blog · 2026-09-04 · Medium · 5 min read · Kubernetes, Sandboxing & isolation, Containers, Linux, Security
  Kubernetes v1.37 promotes the KubeletInUserNamespace feature gate to beta, enabling all node components (kubelet, CRI/OCI runtimes, CNI plugins, kube-proxy) to run as a non-root host user inside a Linux user namespace so container-breakout bugs like runc and containerd CVEs are confined to that account.
- [Zero Trust and Docker Desktop: An Introduction](https://swe.quest/articles/zero-trust-and-docker-desktop-an-introduction-5d503aa79fcd1f57.md): Docker · 2024-08-13 · Easy · 10 min read · Containers, Security, Sandboxing & isolation, Identity & auth, Supply chain security
  Zero Trust principles from NIST SP 800-207 can be applied to Docker Desktop development environments through host hardening, least privilege, network microsegmentation, strong authentication, monitoring and immutability.
- [Docker Best Practices: Understanding the Differences Between ADD and COPY Instructions in Dockerfiles](https://swe.quest/articles/docker-best-practices-understanding-the-differences-between-add-and-copy-4a75b284bd6c2baa.md): Docker · 2024-08-08 · Medium · 6 min read · Containers, Supply chain security
  Dockerfile COPY only copies files and directories from the build context into an image layer.
- [Server-side sandboxing: Containers and seccomp](https://swe.quest/articles/server-side-sandboxing-containers-and-seccomp-0f7ac1ac690426f8.md): Figma · 2023-10-24 · Medium · 15 min read · Sandboxing & isolation, Containers, Linux, Security
  Containers and seccomp offer lighter-weight sandboxing than VMs, but both shift much more configuration responsibility onto the user.
- [Coding Agent Horror Stories: The rm -rf ~/ Incident](https://swe.quest/articles/coding-agent-horror-stories-the-rm-rf-incident-68288dbcab0abba2.md): Docker · 2026-06-01 · Medium · 16 min read · AI agents, Sandboxing & isolation, AI for coding, Virtualization, Security
  An AI coding agent asked to clean up an old repo ran \`rm -rf tests/ patches/ plan/ ~/\`, and the trailing \`~/\` expanded to the user's home directory and wiped an entire Mac, including the Keychain, SSH keys and years of files, with TRIM-enabled SSDs ruling out recovery.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
