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: docker compose up --exit-code-from passes a service's exit code on

Docker
~/tips/tip-compose-up-exit-code-from

docker compose up --exit-code-from tests --build

Runs a stack in the foreground and exits with the exit code of the service you name.

If you run a test or a one-off job with Compose, you want your shell to know whether it passed. --exit-code-from turns a service’s exit code into the exit code of the whole command.

The reference says the option returns the exit code of the selected service container and implies --abort-on-container-exit. That second part means Compose stops all the other containers as soon as any one stops, which is what you want when a database should go away after the test is done.

docker compose up --exit-code-from tests --build
echo $?

Here tests is a service in the file that runs a test suite against the other services, maybe a database and an app. When the tests finish, Compose stops the rest and exits with the code of tests, so 0 means green and anything else means red. In a script or a CI job you can put it behind an if and clean up with docker compose down afterwards.

There is a sibling option, --abort-on-container-failure, which stops everything only when a container exits with a failure.

Both options are incompatible with -d, so you cannot detach, and the output of every service streams to your terminal. I would not use it for a normal long-running stack, since the first container that exits, for whatever reason, takes the rest down with it.

Further reading: docker compose up

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