Skip to content

Lesson 10 of 14 · 15m

Secrets That Stay Secret on One Box

Why env vars are readable process metadata, why secrets never bake into images, and /run/secrets + sops as the homelab-sized fix.

Your docker-compose.yml belongs in git. The .env sitting next to it is the crown jewels — and "environment variable" is where secrets go to be read by everyone with root or docker access on the box.

Env vars are process metadata, not a vault

Run docker inspect app and read Config.Env — your database password, in cleartext, for anyone in the docker group. The same values sit in /proc/<pid>/environ, root-readable. Child processes inherit the environment. The wrong mental model is "the variable stays inside the container": it stays inside nothing — it's kernel-visible process state with several read paths.

Baseline practice for a single box: the .env (or env_file) file is chmod 600, owned by the deploy user, and .gitignored. The compose file tracks structure only — service names, networks, mounts — never values.

Secrets don't belong in images, ever

ENV/ARG in a Dockerfile, a secret copied into a build, a config baked into layer three — all of it ships with the image. docker history --no-trunc narrates the build; docker save + tar -x extracts every layer. "Private registry" doesn't rescue this: anyone who can pull can read. Image = publishable. Secret = injectable at runtime. Those are disjoint sets.

The two upgrades that fit a homelab

Compose secrets: mounts a host file into the container at /run/secrets/<name> on a tmpfs — the app reads a file, not an environment variable, so docker inspect and /proc/*/environ go clean:

services:
  db:
    secrets: [pg_password]
secrets:
  pg_password: { file: ./secrets/pg_password }

The host file is chmod 600 and gitignored — same discipline as .env, minus the env-var exposure. Postgres images honor _FILE suffixes (POSTGRES_PASSWORD_FILE) so this works without app code changes.

sops + age for the at-rest copy: Mozilla's sops encrypts values inside a file (YAML/JSON/env), age provides the keys — sops -e .env > .env.enc produces ciphertext you can commit, because the decryption key lives only on the box and your laptop. Deploy = sops -d .env.enc > .env && docker compose up -d. This solves the backup problem from last lesson: config goes in git, secrets go in git encrypted, and the restore drill never hits "the .env only existed on the dead box."

The footgun: serving .env to the internet

If nginx has a root anywhere near your project directory, GET /.env is a real request — botnets scan it on every host on earth, and leaked .env files are a documented mass-compromise vector. The rule is structural: location ~ /\. { deny all; } on anything serving files, and deploy directories that hold secrets should never live under a docroot at all.

Rotation is a procedure, not a panic

Plan rotation before the incident: secrets in a file swap with a restart (compose up -d recreates the env); database passwords are the painful one — ALTER ROLE then update the file then restart, in that order. Rehearse once on purpose. A secret you've never rotated is a secret you'll fumble rotating at 3 a.m. with an attacker reading your env_file.

Field notes

  • .gitignore both .env and secrets/ — and verify with git status before the first commit, not after.
  • git log -p -- .env answers "did I ever commit this" — the answer you don't want means rotate everything in it, because git never forgets.
  • Provider-level secret managers (Vault, Doppler, SSM) exist for fleets; on one box they're ops burden disproportionate to the gain. Files with correct permissions are the honest answer at this scale.
  • chmod 600 isn't ceremony: on a multi-service box it keeps the postgres container's env readable only by the user who deploys it, not every daemon's crash dump.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.