Sub topic 5
Mapping ports: fixed and dynamic
This chapter assumes you can read a GitHub Actions workflow, know basic Docker port publishing, and have already declared a service with the services key. Here you decide when a service needs a ports entry, and how your tests find the port.
When you need ports
Whether you need ports depends on where the job runs.
- If the job runs directly on the runner, the service container lives in Docker's network and your steps live on the host. The only way to reach the service is to publish a container port to the host. You must add
ports. - If the job runs in a container (
container:key), the job and service share a Docker network. You reach the service by its label on the container's own port. Noportsentry is needed.
This chapter covers the first case.
Fixed mapping
A fixed mapping names both the host port and the container port, in the form host:container.
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432Your tests connect to localhost:5432. This is simple and predictable, and the connection string can be written as a constant.
The drawback is that the host port must be free. A GitHub-hosted runner is a fresh machine, so a clash is rare there. On a self-hosted runner, another process or a parallel job may already hold port 5432, and the service fails to start. Two services cannot publish to the same host port either.
Dynamic mapping
A dynamic mapping lists only the container port. Docker picks any free host port.
jobs:
test:
runs-on: ubuntu-latest
services:
redis:
image: redis:7
ports:
- 6379/tcp
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:integration
env:
REDIS_URL: redis://localhost:${{ job.services.redis.ports['6379'] }}Because you do not know the host port in advance, you read it from the job.services context. The expression job.services.redis.ports['6379'] returns the host port that Docker assigned to container port 6379. The key is the container port, written as a string. The service name (redis) must match the key under services.
The diagram below shows the flow.
Choosing between them
| Situation | Choice | Reason |
|---|---|---|
| GitHub-hosted runner, one service per type | Fixed | Simple, port is known |
| Self-hosted runner shared by several jobs | Dynamic | Avoids port clashes |
| Matrix jobs on the same self-hosted machine | Dynamic | Each job gets its own port |
| Job runs in a container | Neither | Use the label and default port |
Common errors
- Using
job.servicesin a place where it is not available. Use it in stepenvorruncommands, not inservicesitself. - Writing the host port when you meant the container port as the key, such as
ports['5432']after mapping5433:5432. The key is always the container port. - Using
localhostwith acontainer:job. Inside a job container,localhostis the job container itself, not the service. - Forgetting
portson a runner job and gettingconnection refused.
Exercise
Exercise
A job runs on ubuntu-latest with no container: key. Its PostgreSQL service lists ports: - 5432. Which expression gives the host port to put in DATABASE_URL, and what host name do you use?
Show answer
Use ${{ job.services.postgres.ports['5432'] }} for the port and localhost for the host name. Listing only the container port makes the mapping dynamic, so Docker picks the host port and the job.services context exposes it.
Exercise
Why can a fixed mapping of 5432:5432 fail on a self-hosted runner but rarely on a GitHub-hosted one?
Show answer
A GitHub-hosted runner is a fresh virtual machine, so port 5432 is free. A self-hosted runner may already have a process or another job using that host port, so Docker cannot bind it.