DEV Community

Cover image for Use Docker Compose Overrides for Multiple Environments
Keyur Ramoliya
Keyur Ramoliya

Posted on

Use Docker Compose Overrides for Multiple Environments

When working on a project with multiple environments (e.g., development, testing, production), Docker Compose overrides can simplify the management of environment-specific configurations. Docker Compose allows you to define a base docker-compose.yml file with common configurations and then use override files to specify environment-specific settings.

Here's how you can structure your Docker Compose files:

  • Create a base docker-compose.yml file with shared services and configurations:
version: '3'
services:
  webapp:
    image: my-webapp-image
    ports:
      - "80:80"
    environment:
      - DB_HOST=db
      - DB_PORT=5432
      - DB_USER=myuser
      - DB_PASSWORD=mypassword
Enter fullscreen mode Exit fullscreen mode
  • Create separate override files for each environment, such as docker-compose.dev.yml, docker-compose.test.yml, and docker-compose.prod.yml. In these override files, you can specify environment-specific configurations:
# docker-compose.dev.yml
version: '3'
services:
  webapp:
    environment:
      - DEBUG=true
      - LOG_LEVEL=debug

# docker-compose.test.yml
version: '3'
services:
  webapp:
    environment:
      - DEBUG=false
      - LOG_LEVEL=info
Enter fullscreen mode Exit fullscreen mode
  • When you want to spin up your application in a specific environment, use the -f flag with docker-compose to specify the base file and one or more override files:
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d
Enter fullscreen mode Exit fullscreen mode

This command combines the configurations from docker-compose.yml and docker-compose.dev.yml, overriding all overlapping settings. This way, you can keep your base configuration clean and maintain environment-specific settings in separate files.

Using Docker Compose overrides helps streamline your Docker setup for different environments and makes it easier to manage variations in configurations, environment variables, and services between development, testing, and production environments.

Top comments (0)