Self-Hosting · Security

Secrets and .env Hygiene for Docker Stacks

Every self-hosted stack accumulates passwords: database credentials, admin accounts, API keys, VPN logins. The default place they land — plaintext, inside the Compose file — is also the file you paste into forum posts, commit to git, and back up to three clouds. This guide lays out the practical hierarchy for keeping them: what .env files do and don’t protect, when Docker’s secret mechanisms earn their complexity, and the habits that matter most at home.

Published Nov 20, 2026

How Compose handles secrets today

Three mechanisms, in increasing strength. Environment variables inline in the Compose file: simplest, and the worst — the value lives in the file you share, and it is visible to anything that can run docker inspect or read the container’s environment. A .env file next to the Compose file, referenced by variable substitution or env_file:: the credentials leave the Compose file (which becomes safe to share and commit), but they still sit in plaintext on disk and still land in the container’s environment. File-based secrets: Compose’s top-level secrets: with a file source mounts each secret as a file under /run/secrets/ inside the container, and well-built apps read them via _FILE-suffixed variables (the official Postgres and MariaDB images support this pattern) — the value never appears in the environment or the Compose file at all.

The realistic home standard

You do not need a vault service to run a tidy home stack. The standard that covers most risk: one .env file per stack, stored with the stack, holding every credential that stack needs; the Compose file references variables only. Set the file’s permissions to owner-only (chmod 600). Generate real passwords — your password manager (self-hosted via Vaultwarden, perhaps) is right there — instead of reusing one “homelab password” across every database you run. Reuse is the actual catastrophe multiplier: one leaked stack file should compromise one service, not all of them.

Graduate the important few to file secrets: the database passwords of the apps you cannot rebuild (Immich, Paperless, Nextcloud) are worth the extra lines, because their images support _FILE natively. For everything else, a permissioned .env is a defensible setup — the remaining exposure is someone with shell access to the host, at which point the secrets were the least of the problem.

Git: where secrets actually leak

The classic self-hosting leak is not a hacker; it is your own dotfiles repo. If your stacks live in git, three rules keep credentials out of history: a .gitignore that excludes .env from the first commit (not after the first leak — git history remembers), a committed .env.example with placeholder values so the structure is documented without the secrets, and a search of the repo for your real passwords before it goes anywhere public. If a real secret ever lands in a public repo, rotate the credential — deleting the commit is not enough, because copies and caches persist.

Sharing Compose files for help follows the same hygiene: paste the Compose file freely (it holds no secrets by design now), never the .env, and redact hostnames and IPs only if you care about the map of your network — the exposure guide explains why that map is worth some discretion.

Backups and rotation

Your backup strategy must include the .env files and secret files — a stack restored without its database password is a restore that fails at the last step — and must protect them like the data they unlock (encrypted backup targets, as in our Duplicati guide). Finally, rotation: when you decommission a service, when a credential appears anywhere it shouldn’t, or when someone who knew it shouldn’t anymore, change it. Keep a line per stack in your inventory naming where its secrets live; future-you, mid-incident, will be grateful.

Official documentation and references

Frequently asked questions

Is a .env file actually secure?

It is a separation mechanism, not encryption: values sit in plaintext on disk and are visible in the container’s environment. What it buys is real — credentials leave the shareable Compose file, stay out of git by default with a .gitignore, and can be permissioned to the owner only. Against the realistic home threats (accidental sharing, repo leaks, one app reading another’s config), that covers most of the risk. File-based secrets close the environment-visibility gap for apps that support them.

What is the _FILE pattern?

Many official images accept a variable like POSTGRES_PASSWORD_FILE pointing at a file path instead of POSTGRES_PASSWORD with the value. Combined with Compose secrets, the password is mounted as a file at /run/secrets/… and never appears in the container’s environment, in docker inspect output, or in your Compose file. Check an image’s documentation page for the _FILE variables it supports.

I already committed a real password to a public repo. What now?

Rotate that credential immediately — assume it is public forever, because scrapers harvest pushed secrets within minutes and removing the commit does not un-publish it. Then scrub the repo history if you care about tidiness, add the .gitignore rules, and switch the stack to .env-based variables so the replacement secret never enters the repo.

Should I run a dedicated secrets manager like Vault or Infisical?

For a home stack, usually not yet — it is one more critical service to run, back up, and bootstrap (including the chicken-and-egg problem of where its own credentials live). The .env-plus-file-secrets standard in this guide covers home risk well. A secrets manager earns its place when multiple people or machines need shared, audited access to the same credentials.

Related guides