改善
Kaizen  · Today I Learned by Ville Säävuori

Using Docker Compose In GitHub Actions

Running non-trivial Django applications in CI (for example e2e testing purposes) has always been a big painpoint for me. For some reason using Docker Compose never even occured to me until yesterday when I saw an example of it in the wild. Few hours later I had converted one of my own projects and it workd great.

I have docker-compose.yml with a section like this:

testdjango:
  build:
    context: .
    dockerfile: ./conf/dev.Dockerfile
  volumes:
    - .:/code/:delegated
  ports:
    - 3000:3000
    - 8000:8000
  depends_on:
    - db
    - redis
  env_file:
    - .env.github
  command: pytest

To make working with env variables for the test container I added the extra .env.github and committed it in. Then, I just added the following action, which Just Works:

name: Test Backend

on:
  pull_request:
  workflow_dispatch:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Run pytest
        run: docker compose run testdjango

This way I get the full benefit of using my own (private) containers, the same setup from Docker Compose I use for local development, and all the extra services like databases as well.

There are several examples of all kinds of complicated build setups for using your own dockerfile in GH Actions but this is by far the simplest way to do it IMO.