Skip
Arish's avatar

26. Environment and Override Files


Environment Files

bash
1# .env file (loaded automatically)
2POSTGRES_PASSWORD=secret
3RAILS_ENV=development
yaml
1# docker-compose.yml
2services:
3  db:
4    image: postgres:15
5    environment:
6      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}

Override Files

yaml
1# docker-compose.yml - base config
2services:
3  web:
4    build: .
5    ports:
6      - "3000:3000"
7
8# docker-compose.override.yml - dev overrides (auto-loaded)
9services:
10  web:
11    volumes:
12      - .:/app
13    environment:
14      - RAILS_ENV=development

Production Config

yaml
1# docker-compose.prod.yml
2services:
3  web:
4    image: myregistry/myapp:latest
5    environment:
6      - RAILS_ENV=production
7    restart: always
bash
1# Use production config
2docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d

Multiple Environments

bash
1# Development (uses override automatically)
2docker compose up
3
4# Production
5docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d
6
7# Testing
8docker compose -f docker-compose.yml -f docker-compose.test.yml up