How to finish docker-compose action and mark as succes


I have a pyhon project that runs a couple of containers: nginx, uwsgi, mysql, celery, rabbitmq.
And another container to run integration tests (with chromedriver).
This all works fine as docker-compose is supported by GitHub Actions via:

name: Docker Compose Actions Workflow
on: push
    runs-on: ubuntu-latest
      - uses: actions/checkout@v2
      - name: Set integration-test mode
        run: docker/ integration_tests
      - name: Build the stack
        run: docker-compose up --build

This builds the containers, starts them and runs the integration tests as part of the entrypoint script of the integration-tests container.

But after finishing the tests, all containers keep running, or at leasts the ones running a service such as nginx, uwsgi, celery, mysql.
How can I mark the GitHub Action (step) as successful after completing the integration tests. And make sure the containers are stopped and the action continues?


docker-compose up will by default stay attached to the started container, so it won’t terminate as long as they are running. In principle the way to get around it is to use docker-compose up -d to detach. The tricky thing is that with -d the up command will terminate very quickly (as soon as the containers are up), likely before your tests are complete.

So you could use a sequence like this:

      - name: Build the stack
        run: docker-compose up --build -d
      - name: Get test result
        run: |
          # wait for the test results, and terminate with 
          # a non-zero exit code if they are bad
      - name: Stop containers
        # the containers should be stopped regardless of 
        # the test result
        if: always()
        run: docker-compose down

My first try was with ‘up -d’ which kills the containers too soon indeed. Didn’t want to with some strange dev/null tail in my Dockerfile as suggested elsewhere.
I now have added

 - name: Logs
       run: docker-compose logs -f integrationtest

Which seems to correctly wait until the integrationtest container finishes. That container exist/fails with exit code 1 due to a failing test. But the github action is marked as succesful. Not sure how this works, the whole docker-compose setup seems to be undocumented for GitHub actions? Github Actions as Status checks?

Now trying your suggestion, build takes 30 minutes. Not sure what the single ‘|’ command does?

Tried with the suggested '|'command, but that doesn’t seem to do anything but instantly complete. So containers are killed, no integration tests are run. Probably need to use docker inspect or something to get the exit code.

That’s probably because reading the log (what docker-compose logs does) was successful. That’s why I had the “wait for the test results, and terminate with a non-zero exit code if they are bad” comment in my example. I assumed you had some process to retrieve the result from the container (e.g. parse a test log or something).

If you just need the exit code: I’ve done something similar before where I was running the test container independent of docker-compose, and just attaching it to the same virtual network so it could talk to the service containers. That way I could just use the exit code from the docker run command. Depending on how tightly your tests are coupled with the network structure you may be able to do the same by publishing ports and accessing those.

That’s not a command, that’s YAML syntax to start a text block that preserves line breaks. That way you can have more than a one line command in a single run step. :wink:

Example from one of my workflows:

      - name: network overview
        run: |
          ip addr show
          cat /etc/hosts

For more on YAML multiline text, take a look at:

1 Like

Actually I am migrating from Travis and there we are running the integration tests not in a container, but in the main travis script and connecting to the exposes port 8080. I could do the same here I guess and just let the exit code cascade from there. Hopefully it works :slight_smile:

1 Like

That sounds promising, I’d like to hear if it works! :slightly_smiling_face:

I decided to keep the tests running in the container. But started all other containers first with up -d and then start the container with the tests with ‘up --exit-code-from’

      - name: Integration tests
        run: docker-compose up --exit-code-from integrationtest integrationtest

this works and the exit code is cascaded nicely into the GitHub action.

1 Like