Terminate docker compose when test container finishes

前端 未结 6 1132
梦毁少年i
梦毁少年i 2021-01-30 16:56

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 b

6条回答
  •  隐瞒了意图╮
    2021-01-30 17:36

    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.

提交回复
热议问题