brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: depends_on with required: false lets a Compose service start without an optional dependency

Docker
~/tips/tip-docker-compose-depends-on-required-false

required: false

Order an optional service first when it exists, without failing when it does not.

A normal depends_on is strict: if the dependency is missing, Compose refuses. The long syntax has a required field that makes it a soft dependency.

services:
  app:
    image: example/app:1.0
    depends_on:
      cache:
        condition: service_started
        required: false
  cache:
    image: redis:7-alpine
    profiles: ["cache"]

When the cache profile is active, Compose starts Redis before the app, as usual. When it is not, the app starts on its own and Compose only prints a warning instead of an error. The default is required: true.

I like this together with profiles. Optional extras such as a cache, a metrics exporter or an admin UI can be switched on per machine, and the main service does not care either way.

The app itself still has to cope when the cache is not there. Compose only handles the start order; if the code crashes when it cannot reach Redis, required: false just moves the error from Compose to your logs. The field needs a reasonably recent Compose version, so check with docker compose config that yours keeps it.

Further reading: Compose file reference: depends_on

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link