Terminate docker compose when test container finishes

You can use these docker-compose parameters to achieve that:

--abort-on-container-exit Stops all containers if any container was
stopped.

--exit-code-from Return the exit code of the selected service
container.

For example, having this docker-compose.yml:

version: '2.3'

services:
  elasticsearch:
    ...
  service-api:
    ...
  service-test:
    ...
    depends_on:
      - elasticsearch
      - service-api

The following command ensures that elasticsearch and service-api go down after service-test is finished, and returns the exit code from the service-test container:

docker-compose -f docker-compose.yml up \
    --abort-on-container-exit \
    --exit-code-from service-test

Leave a Comment