Skip to content
rusthacks.dev
DevOps

Running Rust Containers as a Non Root User Safely Explained

Containers run as root by default. Create an unprivileged user in the image, own only what the app writes, handle low ports and volumes, and check that it works.

3 min read
Summarize in AI
Flat illustration for the post “Running Rust Containers as a Non Root User Safely Explained”, composed around deploy.

The default user in a container is root. If the Rust service has a vulnerability, the attacker runs as root inside the container, with the ability to modify everything in it and, with a misconfiguration, more. Dropping to an unprivileged user takes four lines and closes much of that. The things that go wrong are file ownership and ports. This post covers both. It is the hardening step of the packaging posts in a small Docker image for Rust.

Docker, Rust 1.90.

The Dockerfile

DockerfileDockerfile
FROM rust:1.90-bookworm AS builder
WORKDIR /app
COPY . .
RUN cargo build --release --locked

FROM debian:bookworm-slim
RUN useradd --system --uid 10001 --no-create-home --shell /usr/sbin/nologin app \
    && mkdir /data && chown app:app /data
COPY --from=builder --chown=root:root /app/target/release/myapp /usr/local/bin/myapp
USER 10001:10001
WORKDIR /data
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/myapp"]

The binary is owned by root and not writable by the app user: a compromised process cannot replace its own executable. /data is the one directory the app owns. USER 10001:10001 uses numbers so that a Kubernetes securityContext with runAsNonRoot: true can check without needing /etc/passwd.

Verify

Shell
docker run --rm --entrypoint id myapp:dev
docker run --rm --entrypoint sh myapp:dev -c 'touch /usr/local/bin/x'

The first prints uid=10001(app) gid=10001(app) groups=10001(app). The second fails with Permission denied, which is the point. On distroless images without a shell, inspect with docker inspect --format '{{.Config.User}}' myapp:dev.

Ports

An unprivileged process cannot bind ports below 1024. Listen on 8080 or 3000 inside the container and map it outside:

Shell
docker run -p 80:8080 myapp:dev

Binding to 0.0.0.0:8080 inside the container is needed for the published port to reach it; 127.0.0.1 inside the container means only the container itself.

Writable paths

Apps that write temporary files, caches or local SQLite need a writable directory. Options, in order of preference: a named volume mounted at a path the user owns, tmpfs for /tmp (docker run --tmpfs /tmp), or a directory created and chowned at image build time. With a read-only root file system (--read-only), these are the only writable places, and the service should fail to start if it needs to write anywhere else.

Volumes with host paths

A bind mount keeps the host's ownership. If ./data is owned by your user (UID 1000) and the container runs as 10001, writes fail with Permission denied. Fix on the host with chown 10001:10001 ./data, or use a named volume, which Docker initializes with the image's ownership of the mount point.

Kubernetes

YAML
securityContext:
  runAsNonRoot: true
  runAsUser: 10001
  readOnlyRootFilesystem: true
  allowPrivilegeEscalation: false
  capabilities: { drop: ["ALL"] }

This is the standard hardened profile; a Rust service that listens on a high port and writes to a mounted volume runs under it unchanged.

Limits

A user namespace remap or rootless Docker gives stronger isolation than USER alone. None of this replaces patching: see hardening a Rust Docker image. Docker documents the instruction at Dockerfile USER.

Also inSecurity
Taggeddocker
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.