DEV Community

Cover image for 5 Docker Compose Tricks You Don't Know Yet
Jonas Scholz
Jonas Scholz Subscriber

Posted on

5 Docker Compose Tricks You Don't Know Yet

Docker Compose can feel like a puzzle. You tweak ports for staging vs. production, juggle services you only need sometimes, and replay the “why isn’t this working?” loop when configs drift. It’s tedious.

But what if you could:

  • Swap settings between environments without editing files,
  • Launch only the services you need right now,
  • Preview changes before risking a deploy?

Here are 5 underused Docker Compose features that solve these exact problems. Less friction, more flow. Let's turn you into a Docker Compose wizard! :D

Wizard

1. Variable Substitution

Tired of tweaking your docker-compose.yml for every environment? Enter variable substitution:

services:
  web:
    image: nginx
    ports:
      - "${PORT}:80"
Enter fullscreen mode Exit fullscreen mode

Set PORT=8080 in your environment, and voilà! Your configuration adjusts without a single edit.

2. Profiles

Not all services need to run all the time. Profiles let you pick and choose:

services:
  web:
    image: nginx
    profiles:
      - web
  db:
    image: postgres
    profiles:
      - db
Enter fullscreen mode Exit fullscreen mode

Start just the services you need with:

docker-compose --profile web up
Enter fullscreen mode Exit fullscreen mode

A lot more fun if you have a lot of services than defining them separately in your docker compose up command!

3. Extends

Why repeat yourself when you can extend? Here's how:

services:
  common:
    image: nginx
Enter fullscreen mode Exit fullscreen mode

Then in your docker-compose.yml:

services:
  web:
    extends:
      file: base.yml
      service: common
    ports:
      - "80:80"
Enter fullscreen mode Exit fullscreen mode

Remember DRY (Don't Repeat Yourself)? Turns out that also works for docker compose :D

4. Merge Compose Files

Mix different Docker Compose files like you're crafting a cocktail (or a potion if you're becoming a Docker Wizard):

services:
  web:
    image: nginx
Enter fullscreen mode Exit fullscreen mode

Add some zest with:

services:
  web:
    ports:
      - "8080:80"
Enter fullscreen mode Exit fullscreen mode

Run docker-compose up, and they merge seamlessly. Or, blend for a specific taste:

docker-compose -f docker-compose.yml -f docker-compose.prod.yml up
Enter fullscreen mode Exit fullscreen mode

5. Dry Run Mode

Before you dive headfirst into changes (and potentially a brick wall, aka a production outage), check them out in dry run mode:

docker-compose --dry-run up
Enter fullscreen mode Exit fullscreen mode

This lets you preview the actions Docker Compose would take without actually performing them. Try it out!


These Docker Compose hacks are your secret weapons for a slightly more enjoyable container management experience. Give them a spin and watch your workflow transform!

I hope you learned something new today! Let me know what you think in the comments below :)

Cheers,

Jonas, Co-Founder at Slipane, the easiest way to deploy Docker Containers

Top comments (0)