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)

Most upvoted comments

@sureshsgith Thanks for the info. But here it will wait for service one to gets complete its test before starting the next service two. I need the option to capture exit status from multiple services. Convert --exit-code-from to 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:

Stops all containers if any container was stopped.

This isn’t helpful in the current context because, as described, an exit code of 0 might 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 != 0 for multiple containers, docker compose up works 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:

time ALLOW_DROP_DB=true docker compose --project-name test -f docker-compose.yml up --build --exit-code-from checks --attach checks

I’m not sure if docker compose run would 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.

I’m running tests using compose. I want to report an error if any one of the tests gets failed. Currently, it shows exit code 0.

The closest is compose up --abort-on-container-exit which will report exit code != 0 if any of the service failed

I’m closing this issue as “not planned”

@keith-bennett-airmap the code is in the main branch, so I would expect it in v2.27.0. Or maybe even in v2.26.2.

@Bessonov then just run up with the services you want to focus on:

cat compose.yaml 
services:
  test:
    depends_on:
      - db
    image: alpine
    command: "/bin/true"
  db:
    image: mysql
    environment:
     MYSQL_ROOT_PASSWORD: pass

$ docker compose up --abort-on-container-failure  test
[+] Running 0/2
 ⠙ Container machin-db-1    Created                                                                                                        0.1s 
 ⠋ Container machin-test-1  Created                                                                                                        0.1s 
Attaching to test-1
test-1 exited with code 0

@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.