Sub topic 13
When to use Docker Compose or Testcontainers instead
This chapter assumes you can write a workflow with a services block, know how a runner job differs from a container job, and have run integration tests against a database. Service containers cover many cases. Some cases they handle poorly, and this chapter helps you spot them.
What service containers cannot do well
Service containers are declared in the workflow file and start before the first step. That design causes the limits you will hit:
- They start once per job and stay up until the job ends. You cannot restart one between test files without extra tooling.
- They run only on GitHub-hosted or self-hosted runners that support Docker. Locally, the same setup needs a separate definition.
- You cannot declare startup order between services, and you cannot build an image from a Dockerfile inside the
servicesblock. - The configuration lives in YAML that only the CI system reads. Developers cannot reuse it on their laptops.
If none of these hurt, stay with service containers. They are the shortest path from an empty workflow to a working integration test.
Option 1: Docker Compose
Docker Compose describes a set of containers in a compose.yaml file. You can run the same file on a laptop and in CI.
Choose Compose when:
- Developers already run
docker compose uplocally and you want CI to match. - A service must be built from your own Dockerfile.
- Services depend on each other, for example an API that needs a database and a message broker.
- You need to run commands inside a service, such as a migration tool packaged in its own image.
A workflow step starts the stack and waits for health checks:
name: compose-tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: docker compose up --detach --wait
- run: npm ci
- run: npm run test:integration
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/app_test
- if: failure()
run: docker compose logs
- if: always()
run: docker compose down --volumesThe --wait flag blocks until every service with a health check reports healthy. Your compose.yaml must define a healthcheck for each service, or the flag has nothing to wait for. The if: failure() step prints container logs only when something broke, which replaces the log inspection you would otherwise do by hand.
The cost is that you now manage a second file and the lifecycle commands yourself. Nothing stops the stack automatically except the runner being discarded.
Option 2: Testcontainers
Testcontainers is a library available for Java, Go, Node.js, Python, .NET and other languages. Your test code starts containers itself, and the library removes them when tests finish. No services block is needed.
Choose Testcontainers when:
- Each test class or test file needs a fresh container, which gives stronger isolation than one shared database.
- You want identical behavior on a developer machine and in CI, with no files beyond the test code.
- Ports should be random, with the library reporting the chosen port to your code.
- Different tests need different images or versions inside one job.
The workflow becomes very small, because the runner already has Docker installed:
name: testcontainers-tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run test:integrationThe trade-off is slower tests, since each container start costs seconds, and a language dependency on the library. Failures also show up in test output instead of in the workflow's service setup.
Comparing the three
| Need | Service containers | Docker Compose | Testcontainers |
|---|---|---|---|
| Setup lives in | Workflow YAML | `compose.yaml` | Test code |
| Same setup locally | No | Yes | Yes |
| Build from a Dockerfile | No | Yes | Yes |
| Fresh container per test file | No | No | Yes |
| Startup speed per job | Fast | Medium | Slower with many containers |
Exercise
Your team's tests need a PostgreSQL database that is wiped completely between every test file, and developers want to run the same tests on their laptops without a CI-specific setup. Which option fits best, and why does a plain services block fall short?
Show answer
Testcontainers fits best. The test code starts a new container per test file, so isolation is complete, and the same code runs locally. A services block starts one container per job that lives until the job ends, so data persists across test files unless you add cleanup yourself, and the definition exists only in the workflow YAML.