Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminate docker compose when test container finishes

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}" 
like image 669
David Karlsson Avatar asked Dec 01 '16 10:12

David Karlsson


People also ask

How do I stop docker compose container?

Stop All Docker Containers There are two steps to stop a docker application containers: First, stop the running containers using docker-compose stop. Second, remove the stopped containers using docker-compose rm -f.

How do I stop docker compose container from exiting?

If you want to force Compose to stop and recreate all containers, use the --force-recreate flag. If the process encounters an error, the exit code for this command is 1 . If the process is interrupted using SIGINT (ctrl + C) or SIGTERM , the containers are stopped, and the exit code is 0 .

Do I lose my data when the docker container exits?

Do I lose my data when the container exits? 🔗 Not at all! Any data that your application writes to disk gets preserved in its container until you explicitly delete the container.


2 Answers

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 
like image 100
Michael Spector Avatar answered Oct 06 '22 07:10

Michael Spector


Compose has added the --exit-code-from {container} flag to docker-compose up which makes this easier.

docker-compose up --exit-code-from test-runner 

See Michael Spector's answer for more detail.


Original Answer

Similar to this rspec q/a, you need to run the tests as a standalone task that report an exit status back to your CI.

You could separate the test-runner into it's own yaml or modify the test-runner to default to a no op command/entrypoint.

Separate the test runner

Specify the test-runner config separately (You might need to upgrade to version 2 networks instead of using links to work across multiple compose files).

docker-compose up -d docker-compose -f test-runner.yml run test-runner rc=$? docker-compose down exit $rc 

No op test runner

Default the test-runner to a no op entrypoint/command and then manually run the test command

services:   test-runner:     image: "${RUNNER_IMG}"     command: 'true' 

Then

docker-compose up -d docker-compose run test-runner /launch-tests.sh rc=$? docker-compose down exit $rc 

Return codes

If your CI has the concept of "post tasks" you might be able to skip the rc capturing and just run the docker-compose down after the test-runner CI task has completed. It's also possible your CI cleans up the containers for you.

like image 24
Matt Avatar answered Oct 06 '22 08:10

Matt