3/3
Environment Variables & Configuration
+15 XP on completion
#Environment Variables & Configuration
After this lesson you'll know:
- how to manage env vars safely
- env_file vs environment
- profiles for different setups
#Three ways to set env vars
services:
api:
# 1. Directly in YAML
environment:
DEBUG: "true"
NODE_ENV: production
# 2. From a file
env_file: .env.production
# 3. From the host machine
environment:
- SECRET_KEY # Gets the value from the host environment
#env_file
# .env (automatically loaded by compose)
POSTGRES_PASSWORD=***
POSTGRES_USER=app
# .env.production (specified manually)
NODE_ENV=production
API_PORT=3000
#Profiles
With profiles you can define different setups for dev and prod:
services:
api:
image: my-api
profiles: ["dev", "prod"]
mailpit:
image: axllent/mailpit
profiles: ["dev"] # Dev only
prometheus:
image: prom/prometheus
profiles: ["prod"] # Prod only
# Dev services only
docker compose --profile dev up -d
# Prod services only
docker compose --profile prod up -d
#โ Try it out
- Create a compose.yml with two profiles:
dev(web + db) andprod(web + db + redis). Start only dev with--profile dev - Create a .env file with POSTGRES_PASSWORD and use ${POSTGRES_PASSWORD} in your compose.yml
#๐ Summary
- Three ways for env vars: environment directly, env_file, or host variables
- Profiles allow different setups for dev and prod
- The .env file is automatically loaded by Compose
Compose Challenge
Test your knowledge with a quick quiz!
4 questions ยท +40 XP