Sub topic 11
Limitations to know about
This chapter assumes you can read a workflow file, know basic Docker concepts (images, ports, environment variables), and have already declared a service with the services key. Service containers are convenient, but they have fixed boundaries. Knowing them early saves you from designing a test setup that the feature cannot support.
What service containers are built for
A service container is started by the runner before your steps run, and it is removed when the job ends. That lifecycle explains most of the limits below. The runner treats the service as part of the job's environment, not as something you control step by step.
Limits that come from the lifecycle
- You cannot restart or reconfigure a service between steps. The configuration is fixed when the job starts. A step cannot change an environment variable on the running service or stop it and start it again with different settings.
- Services exist only for one job. Each job gets its own containers. Two jobs in the same workflow cannot share one database, and data written in one job is gone in the next. Pass files between jobs with artifacts, not through a service.
- Startup order is not configurable. Services start before your steps, but you cannot say that service B waits for service A. If one service depends on another, handle that inside the application or with health checks in your test setup.
- The job does not run your service's command. You can only set the image, environment, ports, volumes, and
options. Theoptionsfield accepts a limited set of Docker create options, so you cannot reproduce everydocker runflag.
Limits that come from the runner
Service containers need Docker on the runner, so they depend on where the job runs.
| Runner | Service containers | Notes |
|---|---|---|
| Linux, GitHub-hosted | Supported | Docker is preinstalled. |
| Windows or macOS, GitHub-hosted | Not supported | Use a Linux runner, or install the dependency as a normal step. |
| Self-hosted Linux | Supported | Docker must be installed and the runner user must be allowed to use it. |
Because the container is created on the runner, its image must be pullable from there. Docker Hub rate limits and slow pulls count against your job time. Large images such as a full Elasticsearch image can add a noticeable delay before your first step.
A service container is also only as large as the runner. Memory and CPU are shared with your build and tests, so a heavy database can slow tests or cause out-of-memory failures that look like random test crashes.
Limits that come from the job model
- Containers are not reachable from other jobs or other machines. The service is available only to steps in its own job.
- Services cannot be built from a Dockerfile. The
imagekey needs an image that already exists in a registry. If you need a custom image, build and push it first, or consider Docker Compose (covered in a later chapter). - Files in your repository are not automatically inside the service. To load an initialization script, mount it with
volumes, which requires a path that exists on the runner before the services start. Because the checkout step runs after services start, a script from your repository is often not there yet. Load such data from a step instead, using a client command or your migration tool. - Logs are not shown inline. A failing service does not print into your step output. You must look at the job's "Initialize containers" section or collect logs yourself.
Working around a limit
When a service must change between test phases, use separate jobs. Each job starts its own clean service with its own configuration.
jobs:
test-default-config:
runs-on: ubuntu-latest
services:
redis:
image: redis:7
ports:
- 6379:6379
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:integration
test-with-password:
runs-on: ubuntu-latest
services:
redis:
image: redis:7
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:integrationEach job runs in parallel and gets an isolated service. This costs more runner minutes but keeps the tests independent.
Deciding if the limits matter
If your needs fit one or two standard images, a clean service per job, and a Linux runner, none of these limits will bother you. If you need custom images, several coordinated services, or a stack you can also run locally, the limits point toward the alternatives in the final chapter.
Exercise
Your integration tests need a PostgreSQL service loaded with an SQL file from your repository. You mount the file with a volumes entry, but the database starts empty. Why, and what is a reliable fix?
Show answer
Services start before the checkout step, so the file does not exist on the runner when the container is created and the mount has nothing useful in it. Load the data from a step after checkout instead, for example by running psql with the file or by running your migration and seed command.
Exercise
A workflow has two jobs. The first writes rows to a service container database. The second expects to read those rows. Why does the second job find nothing?
Show answer
Each job starts its own service containers and removes them when it ends, so the second job gets a fresh, empty database. Combine the work in one job, or pass data between jobs as an artifact.