Using Docker Compose in GitHub Workflows#

GitHub Actions is a powerful tool for automating your CI/CD pipelines. It allows you to run your tests, build your code, and deploy your applications with ease. One of the most common use cases for GitHub Actions is building and testing Docker images. The example below shows how to use Docker Compose in a GitHub Workflow to build and test a Docker image when the docker-compose.test.yml file is present that contains the test configuration.

Example .github/workflows/docker-image.yml#
---
name: Docker Image CI

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:

  build-test:
    name: Container Test Build
    runs-on: ubuntu-latest

    steps:
      - name: Checkout the code
        uses: actions/checkout@v4

      - name: Run tests
        run: |
          if [ -f docker-compose.test.yml ]; then
            docker-compose --file docker-compose.test.yml build
            docker-compose --file docker-compose.test.yml run sut
          else
            docker build . --file Dockerfile
          fi

This run a seperate executable called docker-compose to build and run the tests, but the error message shows that the command is not found. The error message is shown below:

Error Message from GitHub Actions#
Run if [ -f docker-compose.test.yml ]; then
  if [ -f docker-compose.test.yml ]; then
    docker-compose --file docker-compose.test.yml build
    docker-compose --file docker-compose.test.yml run sut
  else
    docker build . --file Dockerfile
  fi
  shell: /usr/bin/bash -e {0}
/home/runner/work/_temp/07e35742-59f1-4b8f-ba2f-eb70a45ff88e.sh: line 2: docker-compose: command not found
Error: Process completed with exit code 127.

The error message is clear that the docker-compose command is not found. This is because the docker-compose command is not installed in the GitHub Actions runner. To fix this issue the subcommand docker compose should be used instead of docker-compose. The updated GitHub Workflow is shown below:

Example .github/workflows/docker-image.yml#
---
name: Docker Image CI

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:

  build-test:
    name: Container Test Build
    runs-on: ubuntu-latest

    steps:
      - name: Checkout the code
        uses: actions/checkout@v4

      - name: Run tests
        run: |
          if [ -f docker-compose.test.yml ]; then
            docker compose --file docker-compose.test.yml build
            docker compose --file docker-compose.test.yml run sut
          else
            docker build . --file Dockerfile
          fi

With this Docker follows a similar approach to Podman, where the podman-compose command is also being replaced with podman compose. This change allows the GitHub Workflow to build and test the Docker image successfully and the error message is no longer present.