USER
What is USER?
USER: Sets the user (and optionally group) that RUN, CMD, and ENTRYPOINT run as. Use it to avoid running as root. USER in a Dockerfile sets which user the following RUN steps, and the container's main process, run as. USER app switches from root to a normal user named app. By default, containers run as root. If an attacker finds a hole in your application, they then have root rights inside the container, which makes escaping to the host or abusing mounted files much easier.
Frequently asked questions about USER
What should you keep in mind with USER?
Create a dedicated user in the image, give it ownership of only the folders it needs to write, such as storage and cache, and switch to it before the final command. Many official images already include a suitable user, like node or www-data. Kubernetes can enforce this with runAsNonRoot: true in the security context, which refuses to start containers that would run as root.
What does an example of USER look like?
RUN adduser -D app USER app At a glance
| Category | Docker |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |