Interview Prep/Docker/Networking and Compose

How do you handle secrets in Compose?

IntermediateScenarioNetworking and Compose
Answer

Situation read → Env vars leak in docker inspect and logs.

What I'd do →

services:
  api:
    secrets: [db_password]
    environment: { DB_PASSWORD_FILE: /run/secrets/db_password }
secrets:
  db_password: { file: ./secrets/db_password.txt }

App reads the file at start. Keep the secrets directory out of git.

Why → Mounted as a read-only tmpfs file at /run/secrets.

Interview tip → In production use a secret manager (Vault, AWS Secrets Manager).

All Docker questions Roadmap: Docker & containers

More on Networking and Compose