Environment Variable Configuration
Environment Variable Configuration
Variable Substitution Syntax
${VAR}: Docker Compose substitutes with the value of environment variableVAR${VAR-default}: UsesVARif set, otherwise falls back todefaultvalue- Example:
${PORT-3000}uses thePORTenvironment variable or defaults to3000
Setting Environment Variables
Method 1: .env File (Recommended)
Create a .env file in the same directory as your docker-compose.yml:
PORT=5000
DATABASE_URL=postgresql://localhost:5432/myapp
NODE_ENV=production
Method 2: Shell Environment
Export variables before running Docker Compose:
export PORT=5000
export NODE_ENV=production
docker compose up -d
Method 3: Inline Declaration
Set variables for a single command:
PORT=5000 NODE_ENV=production docker compose up -d
Common Patterns
Production Configuration:
services:
app:
ports:
- "${PORT-3000}:3000"
environment:
- NODE_ENV=${NODE_ENV-development}
- DATABASE_URL=${DATABASE_URL}
Corresponding .env file:
PORT=8080
NODE_ENV=production
DATABASE_URL=postgresql://db:5432/prod_db
Best Practices
- Use
.envfiles for project-specific defaults - Document required environment variables in README
- Never commit sensitive values to version control
- Use meaningful default values for development environments
Note: Docker Compose does not support the
-eflag likedocker run. Always use.envfiles or shell environment variables.