Sub topic 2
What a Service Container Is and How Long It Lives
This chapter assumes you can read a GitHub Actions workflow (events, jobs, steps), read YAML, and use Docker basics such as images, ports, and environment variables. It explains what a service container is and when it starts and stops. Later chapters cover the syntax and networking in detail.
What a service container is
A service container is a Docker container that GitHub Actions starts next to your job. It gives your tests a real dependency to talk to, such as PostgreSQL or Redis, instead of a mock or an in-memory fake.
You declare service containers under the services key of a job. Each entry has a label of your choosing (for example postgres) and an image to run. The runner pulls the image and starts the container before your first step.
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:integrationYour tests can then connect to PostgreSQL as they would to a database on a development machine. How they reach it depends on the networking mode, which a later chapter covers.
The lifecycle
A service container belongs to one job. It is not shared between jobs, and it does not exist outside the job that declared it.
- The runner starts the job and creates a Docker network for it.
- The runner pulls each service image and starts each container on that network.
- The runner waits for each container's health check, if you defined one. Without a health check, the runner does not wait for the service to be ready.
- Your steps run, in order.
- When the job ends, whether it passed, failed, or was cancelled, the runner stops and removes the service containers.
Three consequences follow from this lifecycle.
- Every job gets a fresh service. Two jobs that each declare
postgresget two separate databases. Data written in one job is not visible in the other. - Every run starts empty. Nothing persists after the job finishes. Your workflow must create the schema and test data each time.
- Services start before steps. Your first step, including
actions/checkout, runs after the containers have started. Starting is not the same as being ready, which is why health checks matter.
What the service container does not do
A service container only runs the image you give it. It does not run your migrations, create your tables, or load seed data. You do that in steps, after the service is ready.
Service containers also stay up for the whole job. You cannot start one in the middle of a job or stop one early. If a test needs to restart a database, a service container is the wrong tool, and the last chapter covers alternatives.
Services and job scope
Because services are scoped to a job, a workflow with several test jobs pays the start-up cost in each one. If two jobs need the same dependency, declare it in both.
jobs:
unit-and-integration:
runs-on: ubuntu-latest
services:
redis:
image: redis:7
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test
end-to-end:
runs-on: ubuntu-latest
services:
redis:
image: redis:7
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:e2eEach redis here is an independent container with its own empty data.
Exercise
Exercise
A workflow has two jobs, api-tests and worker-tests. Both declare a postgres service. The api-tests job inserts a row into a table. Can worker-tests read that row, and why or why not?
Show answer
No. Each job gets its own service container, created when the job starts and removed when it ends. The two postgres containers are separate instances with separate data, so the row from api-tests does not exist in worker-tests.
Exercise
Your first step runs a migration command and fails with "connection refused", although the postgres service is declared correctly. What part of the lifecycle explains this, and what do you add?
Show answer
The runner starts the container before the steps run but does not wait for PostgreSQL to accept connections unless a health check is defined. The database was still starting when the migration ran. Add health options (such as --health-cmd "pg_isready -U postgres") under options so the runner waits until the service is healthy.