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 restart: true restarts the app when its database is updated

Docker
~/tips/tip-compose-depends-on-restart-true

restart: true

Restarts a compose service whenever Compose updates the service it depends on.

If you recreate a database container, an app that keeps its connection open can end up talking to a dead one. Compose can restart the app for you when the dependency is updated.

It is a field of the long depends_on syntax:

services:
  api:
    image: my-api:latest
    depends_on:
      postgres:
        condition: service_healthy
        restart: true
  postgres:
    image: postgres:17

With restart: true, Compose restarts api after it updates postgres. The docs mention Compose 2.17.0 as the version that introduced it, so run docker compose version if the key is rejected. The service_healthy condition is the other half: it keeps the app from starting before the database accepts connections.

Caveat: it only reacts to a restart that a Compose operation controls, such as docker compose up -d recreating the dependency. If the engine restarts the database after a crash, or you run docker restart by hand, the app is left alone, so keep a reconnect loop in the app for that case.

Without the key, the usual fix is to restart the app yourself after the update with docker compose restart api.

Further reading: Compose workload spec: 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