An .env file stores configuration as environment-variable assignments, such as database usernames and passwords. Moving these values out of application code reduces security exposure and makes server changes easier to maintain. section 3.7
Use an environment file with practical safeguards:
- Reference it from Docker Compose with the env_file directive. section 3.7
- Keep sensitive files out of source control, commonly through Git ignore rules. section 3.7
- Document required variables and setup steps so new instances and team members can configure the system reliably. section 3.7
- Treat the filename as flexible: .env is common, but meaningful alternatives such as .sql_server.conf or webapp.env also work. section 3.7
POSTGRES_USER=postgres POSTGRES_PASSWORD=secret
Shows the compact assignment format used for PostgreSQL configuration values. section 3.7
postgres:
build:
context: ./db
dockerfile: Dockerfile
env_file:
- ./.sql_server.conf
Connects an environment file to a Docker Compose service through env_file. section 3.7
import os … POSTGRES_USER = os.getenv("POSTGRES_USER") POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD") … DATABASE_URL = f"dbname={POSTGRES_USER} user={POSTGRES_USER} ➥ password={POSTGRES_PASSWORD} host=postgres port=5432"
Shows application code reading the variables and assembling a database connection string. section 3.7



