How to stop all containers when one container stops with docker-compose?

DockerDocker Compose

Docker Problem Overview


Up until recently, when one was doing docker-compose up for a bunch of containers and one of the started containers stopped, all of the containers were stopped. This is not the case anymore since https://github.com/docker/compose/issues/741 and this is a really annoying for us: We use docker-compose to run selenium tests which means starting application server, starting selenium hub + nodes, starting tests driver, then exiting when tests driver stops.

Is there a way to get back old behaviour?

Docker Solutions


Solution 1 - Docker

You can use:

> docker-compose up --abort-on-container-exit

Which will stop all containers if one of your containers stops

Solution 2 - Docker

In your docker compose file, setup your test driver container to depend on other containers (with depends_on parameter). Your docker compose file should look like this:

services:
  application_server:
     ...
  selenium:
     ...
  test_driver:
    entry_point: YOUR_TEST_COMMAND
    depends_on:
      - application_server
      - selenium

With dependencies expressed this way, run:

docker-compose run test_driver

and all the other containers will shut down when the test_driver container is finished.



This solution is an alternative to the docker-compose up --abort-on-container-exit answer. The latter will also shut down all other containers if any of them exits (not only the test driver). It depends on your use case which one is more adequate.

Solution 3 - Docker

Did you try the work around suggested on the link you provided?

Assuming your test script looked similar to this:

$ docker-compose rm -f
$ docker-compose build
$ docker-compose up --timeout 1 --no-build

When the application tests end, compose would exit and the tests finish.

In this case, with the new docker-compose version, change your test container to have a default no-op command (something like echo, or true), and change your test script as follows:

$ docker-compose rm -f
$ docker-compose build
$ docker-compose up --timeout 1 --no-build -d
$ docker-compose run tests test_command...
$ docker-compose stop

Using run allows you to get the exit status from the test run, and you only see the output of the tests (not all the dependencies).

Reference

If this is not acceptable, you could refer to Docker Remote API and watch for the stop event for the containers and act on it.

An example usage is this docker-gen tool written in golang which watches for container start events, to automatically regenerate configuration files.

Solution 4 - Docker

I'm not sure this is the perfect answer to your problem, but maestro for Docker, lets you manage mulitple Docker containers as single unit.

It should feel familiar as you group them using a YAML file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioninsituView Question on Stackoverflow
Solution 1 - DockerMetalstormView Answer on Stackoverflow
Solution 2 - DockerJakub KukulView Answer on Stackoverflow
Solution 3 - DockerVincent De SmetView Answer on Stackoverflow
Solution 4 - DockertacoView Answer on Stackoverflow