ENV File

Difficulty: Intermediate

Question

How do you pass a file of environment variables to a container, and what are the format rules and security concerns?

Answer

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.

Format rules that catch people out

The docker run --env-file parser is deliberately simple:

  • One KEY=value per line. Lines starting with # are comments; blank lines are ignored.
  • Quotes are not stripped. DB_PASS="s3cret" sets the value to "s3cret" including the quote characters. This is the most common bug.
  • No variable expansion. URL=http://$HOST:8080 is passed literally.
  • A line with just KEY (no =) copies that variable's value from your shell, which is handy for passing CI secrets without writing them to the file.
  • No multi-line values.

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.

Compose: env_file versus .env

MechanismPurposeReaches the container?
env_file: [app.env] in a serviceInject variables into that containerYes
environment: in a serviceInject specific variables, overrides env_fileYes
.env next to compose.yamlSubstitute ${VAR} inside the compose file itselfOnly 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.

Security and operational concerns

  • Env vars are not secret. Anyone who can run 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.
  • Never bake them into the image. ENV RAZORPAY_KEY_SECRET=... in a Dockerfile is stored in the image layers and history forever. Pass secrets at runtime.
  • Keep env files out of git. Add *.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.
  • Prefer real secret stores for production. Docker Swarm and Compose secrets mount values as files under /run/secrets/; Kubernetes Secrets, AWS Secrets Manager or Vault are the norm at scale.

Why env files are still useful

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.

How docker run builds the container environment. Later sources win, so a -e flag overrides the same key from a file.
  1. Image ENV defaults: Values set with ENV in the Dockerfile.
  2. --env-file entries: Each file read in order, line by line.
  3. -e / --env flags: Explicit flags override earlier values for the same key.
  4. Process starts with final env: Visible to the app and to docker inspect.

Code examples

Env file and docker run

# 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.

Compose equivalent

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.

Key points

  • docker run --env-file reads KEY=value lines; # starts a comment
  • Quotes are kept literally and $VAR is not expanded with docker run --env-file
  • -e flags override values from --env-file for the same key
  • Compose .env configures the compose file; env_file configures the container
  • Env vars show up in docker inspect, so use secret mounts or a secret manager for sensitive production values

Common follow-up questions

  • How would you pass a database password to a container in production?
  • What is the difference between ARG and ENV in a Dockerfile?
  • Why should .env files also be in .dockerignore?
  • How do Kubernetes ConfigMaps and Secrets map to environment variables?

Interview tips

  • Mention the quoting difference between docker run and Compose; it is a real bug many have hit
  • Raise the docker inspect visibility problem yourself before the interviewer asks about secrets

Concepts covered

--env-file, Environment variables, Compose env_file vs .env, Variable precedence, Secrets handling

More Docker & Containers questions