Sub topic 4
Reaching the Service: Runner Job vs Container Job
This chapter assumes you can read a basic workflow file, know what a Docker port mapping is, and have declared a service with the services key. It covers one question: what hostname and port should your tests use to reach the service?
The answer depends on where the job's steps run. GitHub Actions supports two modes, and picking the wrong address is one of the most common reasons a service "starts fine" but tests cannot connect.
The two modes
A job runs in one of two places:
- On the runner. The steps run directly on the virtual machine (VM) that GitHub provides. This is the default when you set only
runs-on. - In a container. You add
container:to the job, and every step runs inside that image.
Service containers are always Docker containers. What changes is the network the job sits on.
Job on the runner: use localhost and a mapped port
When steps run on the VM, they are outside Docker's private network. The service is reachable only through a port that Docker publishes on the host. So you must:
- Add a
portsentry to the service. - Connect to
localhostusing the host side of that mapping.
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
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/app_test
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:integrationThe mapping 5432:5432 means host port 5432 forwards to container port 5432. Without ports, nothing is published, and localhost:5432 refuses connections.
Job in a container: use the service label
When the job has container:, the job container and the service containers join the same user-defined Docker network. Docker's built-in DNS resolves each service by its label, which is the key under services. Here the label is postgres, so the hostname is postgres.
You connect to the container's own port, so no ports mapping is needed.
jobs:
test:
runs-on: ubuntu-latest
container: node:20
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
env:
DATABASE_URL: postgres://postgres:postgres@postgres:5432/postgres
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:integrationRead the URL carefully: the first postgres is the username and password, and the one after @ is the hostname. The database name defaults to postgres because this example sets no POSTGRES_DB.
Quick comparison
| Row | Job on the runner | Job in a container |
|---|---|---|
| Hostname | `localhost` | The service label, such as `postgres` |
| Port | Host side of the `ports` mapping | The container's own port |
| `ports` needed | Yes | No |
| Name resolution | Host loopback | Docker network DNS |
Common errors
ECONNREFUSED 127.0.0.1:5432in a container job. Inside a container,localhostis the job container itself, not the VM. Use the service label.getaddrinfo ENOTFOUND postgresin a runner job. The label only resolves on the Docker network. Uselocalhostand addports.- Connection refused in a runner job that has
ports. Check that the host port in your URL matches the host side of the mapping. With a dynamic mapping, read the port fromjob.services.<label>.ports. - Works locally, fails in CI. Your local Compose file may use a different hostname than the workflow does. Make the hostname come from an environment variable so each environment can set its own.
Exercise
A job sets container: node:20 and declares a redis service with ports: - 6379:6379. The tests use REDIS_URL=redis://localhost:6379 and fail with connection refused. What is the fix?
Show answer
Inside a container job, localhost refers to the job container, not the VM where the port is published. Change the URL to redis://redis:6379, using the service label as the hostname and the container's own port. The ports mapping is not needed in this mode.