For a recent client project, we wanted to toggle between various Docker Compose profiles to run the project with or without Celery.

Using Compose’s profiles option, we can label services that we may not want to start by default a label. This might look something like this:

services:

  beat:
    profiles:
      - celery
    ...

  celery:
    profiles:
      - celery
    ...


  web:
    ...

We use a casey/just justfile for some of our common workflows, and I realized I could set a COMPOSE_PROFILES environment variable to switch between running a “default” profile and a “celery” profile.

Using just’s env_var_or_default feature, we can set both an ENV variable and a default value to fall back on for our project.

# justfie 

export COMPOSE_PROFILES := env_var_or_default('COMPOSE_PROFILES', 'default')

@up *ARGS:
    docker compose up {{ ARGS }}

# ... the rest of your justfile...

To start our service without Celery, I would run:

$ just up

` To start our service with Celery, I would run:

$ export COMPOSE_PROFILES=celery
$ just up

Our COMPOSE_PROFILES environment variable will get passed into our just up recipe, and if we don’t include one, it will have a default value of default, which will skip running the Celery service.