compose: Fail the docker-compose command if any one of the conatiner failed
Description
I’m using the following docker-compose file
version: '3.4'
services:
one:
image: busybox
command: 'true'
two:
image: busybox
command: 'false'
three:
image: busybox
command: 'true'
I want to report an error If any one of the containers failed. --exit-code-from Only works for selecting a single container.
--abort-on-container-exit will give exit code 0 if service one or three is completed first.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 2
- Comments: 17 (5 by maintainers)
@sureshsgith Thanks for the info. But here it will wait for service
oneto gets complete its test before starting the next servicetwo. I need the option to capture exit status from multiple services. Convert--exit-code-fromto a list or something.Another vote to add something like --abort-on-nonzero-container-exit. I, like @Gobi-Ganesan, am using docker-compose to run unit tests and I run a big matrix of test containers using different dependency combinations. Unfortunately it’s a bit difficult to spot failures because docker-compose up produces exit code zero even if some containers have failing tests.
–exit-code-from works great if you only care about one container having a nonzero exit code. I care about all of my containers having nonzero exit codes.
The script demonstrated by @sureshsgith in OK but forces running all the test containers in serial, not parallel. That would change my run time from about 90s to like 15 minutes.
@ndeloof, I’m facing a similar issue with a comparable use case, and I believe you should reopen the issue. In the core, the question isn’t about returning multiple exit codes but finding a solution for the specific request. It could be an entirely different one, unrelated to the flag.
As per the documentation on
--abort-on-container-exit:This isn’t helpful in the current context because, as described, an exit code of
0might be desired and shouldn’t halt the entire process. The original request is more like “I want my build to fail if (at least) one of the specified containers exits with an error.”We are open to alternative solutions, so let’s focus on finding a solution rather than being tied to a specific proposed fix. Could you please reopen the issue and initiate a discussion on possible solutions to this problem?
@ndeloof It is not only about one-offs; solely one-offs wouldn’t be interesting. In my case, I have a database, start multiple (long-running, at least during the whole process) apps/services, use one-off containers for checks (linting, style checking, code consistency, and consistency of generated artifacts) and tests (unit tests, UI tests). Of course, I have defined dependencies between the containers, and I build some of them during execution, including creating a base image for certain containers. Except for
exit != 0for multiple containers,docker compose upworks really well for me, executing the entire graph in parallel while respecting dependencies. With limit of listening to the failed exit code from one container, my command is as follows:I’m not sure if
docker compose runwould be really helpful in this scenario because it probably imposes a lot of already implemented logic, such as graph-based execution and parallelization on my side.a Posix exit code is by definition an integer, can’t be a “list or something” - you only will get a synthetical result of success or (some) failure, but no detail on which service failed.
The closest is
compose up --abort-on-container-exitwhich will report exit code != 0 if any of the service failedI’m closing this issue as “not planned”
@keith-bennett-airmap the code is in the
mainbranch, so I would expect it in v2.27.0. Or maybe even in v2.26.2.@Bessonov then just run
upwith the services you want to focus on:@ndeloof Thanks, and if the database never stops and all one-shot containers exit with zero, then it blocks forever, right? If it never terminates, then it isn’t useful for most of the use cases mentioned in this issue.
This is quite different from my command above. The command terminates even without
--detach. The only drawback is that it applies to only one container.