Lorixen.
EN

Stop running your app as root inside Docker containers

Short answer: Unless your Dockerfile says otherwise, the app inside a container usually runs as root. If an attacker finds a flaw in your app, root inside the container gives them far more room to do damage. Create an unprivileged user in the Dockerfile and switch to it with the USER instruction, make sure files the app writes to are owned by that user, listen on a port above 1024, and add runtime hardening such as dropping capabilities and no-new-privileges.

Why root in a container matters

Containers isolate processes, but the isolation isn't perfect, and root inside a container still has powers an ordinary user doesn't: it can change any file in the container, install tools, and take advantage of kernel or runtime flaws more easily. OWASP's Docker Security Cheat Sheet calls configuring the container to use an unprivileged user the best way to prevent privilege escalation attacks.

For a small business app, the realistic risk isn't a dramatic container escape every week. It's that one vulnerable dependency, one file-upload bug or one exposed admin panel gives an attacker a foothold, and root makes everything after that easier: tampering with code, reading every secret mounted into the container, or using the container to attack other services.

Step 1: create a user and switch to it

Docker's build best practices say that if a service can run without privileges, you should use USER to change to a non-root user, creating the user and group in the Dockerfile first. The Dockerfile reference explains that USER sets the default user (and optionally group) for the rest of the build stage and for the running container.

The usual shape:

  1. Install packages and do other setup that needs root.
  2. Create a dedicated group and user, for example app, with a fixed user ID.
  3. Copy the application files and set ownership of anything the app must write to.
  4. Switch with USER app before the CMD or ENTRYPOINT.

Many official language images already include an unprivileged user you can switch to, such as a node user in Node.js images. Check your base image's documentation before creating your own.

Step 2: fix file permissions

The most common breakage after switching users is "permission denied":

  • Upload, cache and log folders need to be writable by the app user. Create them in the Dockerfile and chown them to that user.
  • Application code should usually be readable but not writable by the app user, so a compromised process can't rewrite it.
  • Mounted volumes keep the ownership of the host or volume. Make sure the user ID in the container matches what the volume expects, or set ownership when the volume is created.

Avoid the shortcut of making everything world-writable. It undoes much of the benefit.

Step 3: use a high port

Non-root processes can't normally bind to ports below 1024. Run the app on a port such as 3000 or 8080 inside the container and map it to 80 or 443 at the load balancer, reverse proxy or with Docker's port publishing. Nothing outside the container needs to change.

Step 4: don't add sudo

It's tempting to install sudo "just in case". Docker's best-practices guide advises against installing or using sudo in images, noting its unpredictable terminal and signal-forwarding behaviour. If you need root for a setup step at start-up, do it in a small entrypoint that drops to the app user, using a tool designed for that.

Step 5: harden at runtime

Running as a non-root user is the biggest single step. OWASP's cheat sheet lists further rules that are easy to add:

  • Prevent privilege escalation: run containers with --security-opt=no-new-privileges, which stops processes gaining privileges through setuid or setgid programs.
  • Limit capabilities: drop all Linux capabilities and add back only what's needed.
  • Never use privileged mode for application containers.
  • Don't mount the Docker socket into application containers.
  • Read-only filesystem: where possible, run with a read-only root filesystem and mount writable folders only where needed.

In Compose, these map to service options such as user, security_opt, cap_drop and read_only. Orchestrators like Kubernetes have equivalent security settings.

Keep it from slipping back

Once containers run as non-root, protect the change. Add a CI check that inspects the built image's configured user and fails if it's empty or root. Some image scanners and policy tools can do this for you. When someone adds a new service, copy the hardened Dockerfile and Compose settings from an existing one rather than starting from a tutorial that runs everything as root.

Testing the change

  • Run docker exec <container> id to confirm the app runs as the expected user.
  • Exercise every feature that writes files: uploads, exports, caches, logs.
  • Check start-up scripts that may have assumed root.
  • Run your normal test suite against the new image in CI before deploying.

A worked example

Picture a hypothetical Tauranga outdoor-gear rental business whose booking app ran as root in its container. A security review flagged it, along with a mounted Docker socket left over from an old monitoring setup. Their developer removed the socket mount, created an app user in the Dockerfile, moved the app to port 8080, created upload and cache folders owned by that user, and added no-new-privileges and cap_drop: [ALL] in Compose. Two image-upload paths failed in testing with permission errors and were fixed before release.

FAQ

Is rootless Docker the same thing?

No. Rootless mode runs the Docker daemon itself without root. Running your app as a non-root user inside the container is a separate step, and both can be used together.

Will running as non-root slow my app down?

No. It only changes permissions.

What if a package I use needs root?

Check whether it truly needs root at runtime or only during installation. Most only need it during the build.

Do I need to change my Kubernetes manifests?

Setting a non-root user in the image helps; Kubernetes security settings can also enforce it.

If you'd like your containers reviewed for root, capabilities and risky mounts, tell us how they run.

Sources

WhatsAppBook a call