Need a development setup and a test setup for the same app? Copying the entire Compose file for each one creates duplication and invites drift. Keep shared services and settings in a core docker-compose.yml, then put only each environment’s differences in an override file. Compose merges the files in order: settings in later files override settings in earlier ones. section 10.2
Listing 10.1 A Compose override file that updates one property
# from docker-compose.yml - the core app specification:
services:
todo-web:
image: diamol/ch06-todo-list:2e
ports:
- 8080
environment:
- Database:Provider=Sqlite
networks:
- app-net
# and from docker-compose-v2.yml - the version override file:
services:
todo-web:
image: diamol/ch06-todo-list:2e-v2
The core file defines the web service; the override repeats its services and service keys but changes only the image tag. Matching that structure lets Compose apply the change to the existing service. section 10.2
A practical workflow is:
- Define common services in docker-compose.yml. Create docker-compose-dev.yml with just the development changes, such as published ports, a disabled health check, or a network name. section 10.2
- Inspect the merged result before starting containers: docker compose -f ./todo-list/docker-compose.yml -f ./todo-list/docker-compose-v2.yml config. The config command validates the input files and displays the resulting configuration. section 10.2
- Start an environment with its core and override files, plus a distinct project name: docker compose -f ./numbers/docker-compose.yml -f ./numbers/docker-compose-test.yml -p numbers-test up -d. Use the same files and project name when running down. section 10.2
Overrides can change more than an image tag. Use them to add services, adjust ports and storage, or supply application settings through environment variables, environment files, and secrets. Distinct ports and isolated networks let different environments run on one Docker Engine without colliding. The differences remain visible in their respective files, while a shared change needs to be made only once. section 10.5 section 10.2
Configure the to-do app for development
The core file defines the web service and its secret target. A development override publishes port 8089, selects SQLite through an environment variable, loads verbose logging from an env_file, and supplies the secret from a local file. For Windows Server containers, an additional override accounts for different secret file paths. After starting the app, request /list and check the container logs to see the configuration in use. section 10.3
For shorter commands, a Compose environment file can set COMPOSE_FILE and COMPOSE_PROJECT_NAME; the example uses COMPOSE_PATH_SEPARATOR to separate its file names. An override can also carry build settings for a CI pipeline, such as the build context and a build-number argument. Do not confuse a Compose override file, which changes the Compose deployment definition, with a configuration file loaded by the application inside a container. section 10.3 section 11.6 section 18.2
