FrankWiles.com

TIL: Today I Learned

Easy Docker Compose Overrides

I know I’m late to the party on this one. I’ve been using Docker Compose for YEARS but I’ve always hated using override files because I thought the only way to do so was on the command line like:

shell
$ docker compose -f docker-compose.yml -f docker-compose.override.yml up -d

Turns out there is an elgant way to use an override to do something special and not get in your team’s way!

Automatically picking up your override file

If we have compose.yml in the current directory and checked into git we can add a frank-compose.yml file with overrides and have it be automatically used by setting a single environment variable.

shell
$ export COMPOSE_FILE=compose.yml:frank-compose.yml

NOTE: The order feels backwards to me. Your override file must be second which is easy to miss.

We can then add frank-compose.yml to our .gitignore and our team is none the wiser.

I discovered this while trying to set up a somewhat fancy Traefik proxy to be able to run LOTS of compose projects at the same time and have them not run into conflicts with port numbers.

Overriding port numbers

In most of our projects at REVSYS the compose.yaml will have stanzas that read something like this:

yaml
services:
  db:
    # ... 
    ports:
      - "5432:5432" 
    # ... 
  web:
    # ... 
    ports:
      - "8000:8000" 
    # ... 

Which means I’m going to have conflicts all over the place if I don’t override this. Turns out overriding ports isn’t super straight forward. You can’t just not set it, or set it to something like ports: [] you instead have to use this:

yaml

services:
  web:
    ports: !reset [] 
  db:
    ports: !reset []  

Which will ACTUALLY override and empty out the port mappings so you don’t have any conflicts.

Hopefully you find this useful and can adjust your local docer compose configuration however the hell you want without being “that guy” at work who tries to impose his preferences on everyone.

RSS Feed All Content | RSS Feed All TIL