Safeguard
Security Guides•

Which docker run flags harden a model inference container?

Run as a non-root user, mount the root filesystem read-only, drop capabilities, block new privileges and set limits. The flags and the tradeoffs to expect.

Safeguard Security Team
4 min read

Run the container as a non-root user, mount its root filesystem read-only, drop Linux capabilities, set no-new-privileges, limit memory and processes, and avoid privileged mode. Each is a documented docker run option. Model servers need a writable scratch area and GPU access, so plan for those.

Why inference containers deserve this

An inference server parses large untrusted inputs, loads big binary files and often runs with access to expensive hardware. If a bug in the server or a loaded file gives an attacker code execution, the container settings decide how far they get. Hardening flags reduce what a compromised process can touch.

These flags are standard in Docker's command reference. None of them is specific to AI, which is the point: a model server is a normal service and gets normal hardening.

The flags, one by one

FlagWhat the reference says it doesInference note
userSets the user the container runs as, by name or UIDPick a non-root UID that owns the model directory
read-onlyMounts the container root filesystem as read onlyAdd writable mounts for cache and temp
tmpfsMounts a temporary filesystem at a pathGood for temp files and shared memory style needs
cap-dropDrops Linux capabilitiesDrop all, add back only what you can name
security-opt no-new-privilegesStops processes gaining new privilegesThe reference notes tools like su and sudo then stop working
memorySets a memory limitPrevents one container taking the host
pids-limitLimits the number of processesCaps fork based abuse
privilegedGives extended privilegesAvoid it

A starting command

docker run --rm \
  --user 10001:10001 \
  --read-only \
  --tmpfs /tmp \
  --cap-drop ALL \
  --security-opt no-new-privileges=true \
  --memory 48g --pids-limit 512 \
  -v /srv/models:/models:ro \
  -p 127.0.0.1:8000:8000 \
  your-inference-image

Read the choices. The model directory is mounted read-only, so a compromised process cannot rewrite weights. The port is published on localhost, so a proxy on the host decides who gets in. Memory and process limits cap damage from abuse. Adjust sizes to your hardware.

What will break, and how to fix it

  1. Write errors on startup. Servers write caches and temp files. Find the paths in the error, then give those paths a tmpfs or a dedicated writable volume. Do not remove the read-only flag.
  2. Permission errors on the model directory. Make the directory readable by the UID you chose.
  3. GPU access. GPU runtimes need device access. Grant what your runtime documents, test it, and do not reach for privileged mode as a shortcut.
  4. A binary that needs a capability. Add back the specific capability, record why, and review it.

Build the image for it too

Hardening at run time works better with a lean image. Use a pinned base, install only what the server needs, and set a non-root user in the Dockerfile so the default is safe. Scan the image for vulnerable packages before it runs. A separate post on this blog covers scanning a GPU inference image.

Try it yourself

Click through it at your own pace, or open it full screen. Look at how image findings are grouped, then decide which a run time flag cannot fix.

Quick check

Does a read-only root filesystem stop all attacks?

No. It limits persistence and tampering inside the container. A bug can still leak data or abuse memory, so pair it with network and image controls.

Can I just use privileged mode for the GPU?

Avoid it. Grant the specific device access your runtime documents, because privileged mode removes most of the isolation you are trying to add.

Where Safeguard fits

Safeguard scans container images and dependencies and applies policies and gates in CI, so an image with known vulnerable packages can be stopped before it reaches a cluster. A free scan is available.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.