Difficulty: Intermediate
How do you pass a file of environment variables to a container, and what are the format rules and security concerns?
Use docker run --env-file ./app.env myimage. Docker reads the file line by line as KEY=value pairs and sets each one as an environment variable in the container. You can pass --env-file more than once, and an explicit -e KEY=value on the same command overrides a value from the file.
The docker run --env-file parser is deliberately simple:
KEY=value per line. Lines starting with # are comments; blank lines are ignored.DB_PASS="s3cret" sets the value to "s3cret" including the quote characters. This is the most common bug.URL=http://$HOST:8080 is passed literally.KEY (no =) copies that variable's value from your shell, which is handy for passing CI secrets without writing them to the file.Docker Compose's env_file: parser is more forgiving: it strips quotes and supports interpolation. So a file that works with Compose can behave differently with plain docker run.
| Mechanism | Purpose | Reaches the container? |
|---|---|---|
env_file: [app.env] in a service | Inject variables into that container | Yes |
environment: in a service | Inject specific variables, overrides env_file | Yes |
.env next to compose.yaml | Substitute ${VAR} inside the compose file itself | Only if referenced in environment or env_file |
Mixing up the last two is a classic interview question: the project-level .env configures Compose, not your app.
docker inspect sees every value under Config.Env, and they appear in /proc/1/environ inside the container. Crash reporters and debug pages sometimes dump them too.ENV RAZORPAY_KEY_SECRET=... in a Dockerfile is stored in the image layers and history forever. Pass secrets at runtime.*.env to .gitignore, commit an app.env.example with dummy values, and also add them to .dockerignore so COPY . . does not ship them inside the image./run/secrets/; Kubernetes Secrets, AWS Secrets Manager or Vault are the norm at scale.They follow the twelve-factor idea of keeping config outside the image, so the same image runs in dev, staging and production with different files. For local development with a Postgres URL, a feature flag and a sandbox payment key, an env file is exactly the right tool.
# app.env (no quotes, no expansion)
# Database
DATABASE_URL=postgres://app:devpass@db:5432/shop
LOG_LEVEL=info
FEATURE_UPI_AUTOPAY=true
# Take the value from the calling shell (e.g. a CI secret)
PAYMENT_KEY_SECRET
# Run it, overriding one key for this run
export PAYMENT_KEY_SECRET="from-ci-secret-store"
docker run --rm --env-file ./app.env -e LOG_LEVEL=debug alpine env | sort
LOG_LEVEL comes from -e because it overrides the file, and PAYMENT_KEY_SECRET is pulled from the shell because the file lists only the key name.
services:
api:
image: myorg/api:2.0
env_file:
- app.env # injected into the container
environment:
LOG_LEVEL: debug # overrides the value from app.env
In Compose, values under environment take precedence over env_file. A separate .env file beside compose.yaml is only used for substituting variables in the YAML.
--env-file, Environment variables, Compose env_file vs .env, Variable precedence, Secrets handling