Terminate docker compose when test container finishes

David Karlsson picture David Karlsson · Dec 1, 2016 · Viewed 16.9k times · Source

I am currently running a docker-compose stack for basic integration tests with a protractor test runner, a nodejs server serving a web page and a wildfly server serving a java backend.

The stack is run from a dind(docker in docker) container in my build server(concourse ci).

But it appears that the containers does not terminate on finishing the protractor tests.

So since the containers for wildfly, and nodejs are still running the build task never finishes...

How can I make the compose end in success or failure when the tests are finished?

# Test runner
test-runner:
  image: "${RUNNER_IMG}"
  privileged: true
  links:
    - client
    - server
  volumes:
  - /Users/me/frontend_test/client-devops:/protractor/project
  - /dev/shm:/dev/shm
  entrypoint:
    - /entrypoint.sh
    - --baseUrl=http://client:9000/dist/
    - /protractor/conf-dev.js
    - --suite=remember
# Client deployment
client:
  image: "${CLIENT_IMG}"
  links:
    - server
# Server deployment
server:
  image: "${SERVER_IMG}"

Answer

Michael Spector picture Michael Spector · Mar 26, 2018

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