Sub topic 1
Where service containers fit in a workflow
This chapter places service containers inside the structure of a GitHub Actions workflow. Later chapters cover the syntax, networking, and health checks in detail.
Prerequisites
This chapter assumes you already know the following:
- Basic workflow structure in GitHub Actions: events, jobs, and steps.
- YAML syntax, including indentation, lists, and mappings.
- Basic Docker concepts: images, containers, ports, and environment variables.
- How to run automated tests for an application that uses a database or cache.
The problem service containers solve
Integration tests check that your code works with real dependencies, such as PostgreSQL or Redis. On your laptop you probably start those dependencies by hand or with a script. A CI run needs the same dependencies, and it needs them on a fresh machine every time.
A service container is a Docker container that GitHub Actions starts next to your job. The job's steps can use it like any other network service. Actions starts the container before the first step and removes it after the last step.
The three layers you work with
Service containers touch three levels of a workflow. Knowing which level owns what avoids most early confusion.
| Level | What it is | Role for service containers |
|---|---|---|
| Workflow | A YAML file in `.github/workflows` | Defines the events that trigger runs |
| Job | A group of steps that runs on one runner | Owns the `services` key. Each job gets its own services |
| Runner | The machine that executes the job | Hosts Docker, which runs the service containers |
The services key sits at the job level, beside runs-on and steps. It is not a workflow-level setting, and jobs do not share service containers. If two jobs need PostgreSQL, each job declares its own, and each gets a separate instance.
Where the job runs matters
A job can run in two ways, and service containers behave a little differently in each:
- The job runs directly on the runner machine. Steps run on the host, and services are reached through
localhostand a mapped port. - The job runs inside a container, set with the
containerkey. Steps run in a container, and services are reached by their label as a hostname.
The chapter on networking covers both cases. For now, remember that this choice decides which hostname your tests use.
Service containers need Docker on the runner. Linux runners provide it. GitHub-hosted Windows and macOS runners do not support service containers, so use ubuntu-latest or another Linux runner.
A first look
This workflow runs integration tests against a PostgreSQL service. The job runs directly on the runner.
name: integration-tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: app_test
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -U postgres"
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/app_test
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run test:integrationRead it from the top down:
- The workflow triggers on
pushandpull_request. - The
testjob runs on an Ubuntu runner. - Under
services, the labelpostgresnames one service. Itsimage,env, andportslook like the options you would pass todocker run. - The
optionsvalue passes health-check flags to Docker. The job's steps do not start until the check passes. - The job-level
envgives the tests a connection string that points atlocalhost:5432. - The steps check out the code, install dependencies, and run the tests.
The lifecycle in order
The diagram shows when the service exists relative to the steps.
Because the container is removed when the job ends, its data does not persist. Every run starts from an empty service. This keeps runs repeatable, and it means your tests or setup steps must create any schema or data they need.
Common mistakes at this stage
- Placing
servicesat the workflow level. It belongs under a job. - Using a macOS or Windows runner and expecting Docker services to start.
- Assuming a second job can reach the first job's database.
- Skipping the health check, then seeing tests fail because the database was still starting.
Exercise
A workflow has two jobs, unit and integration. Only integration declares a postgres service. Can steps in unit connect to that database? Explain why or why not.
Show answer
No. The services key belongs to a single job. The container starts when integration starts and is removed when it ends. The unit job runs on its own runner and has no access to it.