Skip to content
Blog

Using .env Files for Container Configuration in Docker

Master bulk container configuration using Docker --env-file option, including formatting syntax rules, variable evaluation precedence, secret management, and .dockerignore practices.

Published on • November 17, 2026

AI Assistant

When configuring containerized microservices, supplying dozens of individual -e flags on the command line becomes unmaintainable and prone to human error.

Docker provides the --env-file flag to import environment variables in bulk from external text files. Understanding .env syntax, precedence rules, and secret protection guards against accidental data leaks and configuration drift.


1. Syntax Rules for .env Files

A Docker environment file is a plain text file containing KEY=VALUE pairs formatted according to specific rules.

Basic .env File Example (config.env):

# Database Connection Settings
DB_HOST=postgres.internal.net
DB_PORT=5432
DB_USER=app_user

# White spaces and quotes
APP_NAME="Production Payment Service"
SUPPORT_EMAIL='admin@example.com'

# Comments are ignored when placed at line starts
# EMPTY_VAR will be assigned an empty string
EMPTY_VAR=

# Inherited host variable (no equals sign)
HOST_SECRET_TOKEN

Key Formatting Rules:

  1. Comments: Lines starting with # are ignored as comments. Inline comments after values (e.g., PORT=8080 # http port) are not supported and will be treated as part of the string value.
  2. Quotes: Surrounding values with single (') or double (") quotes preserves literal whitespace, but quotes are considered part of the value unless stripped by the application parser.
  3. Empty Values: Setting KEY= assigns an empty string to the variable inside the container.
  4. Host Inheritance: Specifying KEY without an =VALUE reads the current value of KEY from the host shell.

2. Passing Environment Files with --env-file

To inject an environment file into a container during startup, specify --env-file with the path to your file:

docker run -d --name web-service \
  --env-file ./config.env \
  nginx:alpine

Loading Multiple Environment Files

You can chain multiple --env-file parameters to separate general application settings from environment-specific overrides:

docker run -d --name multi-env-app \
  --env-file ./base.env \
  --env-file ./production.env \
  my-app:v1

If the same variable key exists in multiple files, Docker applies the value from the last specified file.


3. Variable Precedence Rules

When combining -e / --env flags with --env-file and Dockerfile ENV defaults, Docker resolves variable conflicts according to the following strict hierarchy (highest precedence first):

  1. Command-line explicit flags (-e KEY=VALUE / --env KEY=VALUE)
  2. Environment files passed via --env-file (evaluated in order from left to right)
  3. ENV instructions defined inside the Dockerfile
# Explicit -e flag overrides any DB_PORT set inside config.env!
docker run -d --name custom-port-app \
  --env-file ./config.env \
  -e DB_PORT=5433 \
  my-app:latest

4. Security Considerations and .dockerignore

Environment files frequently store sensitive credentials, API keys, and database passwords. Committing .env files to git repositories or building them into Docker image layers creates severe security vulnerabilities.

Best Security Practices:

  1. Add .env to .gitignore and .dockerignore: Ensure .env files are never tracked by source control or bundled into container image context.
# Add to .dockerignore
*.env
.env.*
secrets/
  1. Use Template Files: Commit a .env.example template with fake default values into source control as documentation for other developers.
  2. Use Docker Secrets for Production: For sensitive production deployments, prefer Docker Secrets, Kubernetes Secrets, or dedicated secret managers (e.g., HashiCorp Vault) over plain text .env files.

5. Summary and Best Practices

Takeaway: Simplify container management by consolidating environment variables into .env files loaded via --env-file. Maintain security by adding .env patterns to .dockerignore, using .env.example for team coordination, and leveraging command-line -e flags for high-priority runtime overrides.


Sources