Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 692
insitu Avatar asked Nov 19 '15 09:11

insitu


People also ask

How do you stop all the containers created by docker compose?

In this case, the application will be up and running until you hit Ctrl-C to cancel the foreground process. In this case, when you press Ctrl-C, it is equivalent to executing the “docker-compose stop”. So, it will stop all the containers gracefully.

Does stopping docker stop containers?

The docker stop command attempts to stop a running container first by sending a SIGTERM signal to the root process (PID 1) in the container. If the process hasn't exited within the timeout period a SIGKILL signal will be sent.

How do you remove or stop all containers?

Stop and remove all containers The command docker container ls -aq generates a list of all containers. Once all containers are stopped, remove them using the docker container rm command, followed by the containers ID list.


Video Answer


2 Answers

You can use:

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

Which will stop all containers if one of your containers stops

like image 156
Metalstorm Avatar answered Sep 23 '22 13:09

Metalstorm


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.

like image 37
Jakub Kukul Avatar answered Sep 22 '22 13:09

Jakub Kukul