GitHub Actions service container port not exposed to steps
A service container is reachable from steps that run directly on the runner via a mapped host port (localhost), or by service name when the job itself runs in a container. Missing the ports mapping or using the wrong host makes the connection refuse.
What this error means
A step cannot connect to the service (connection refused) even though the service container is running.
github-actions
services:
redis:
image: redis:7
# no ports mapping declared
steps:
- run: redis-cli -h localhost -p 6379 ping # connection refusedCommon causes
No ports mapping for runner-host access
Steps running on the runner need a host port mapping to reach the service via localhost.
Wrong host for container vs runner job
Container jobs reach services by service name; runner-host jobs reach them on localhost with a mapped port.
How to fix it
Map the port for runner-host steps
- Add a ports mapping to expose the container port on the runner host.
- Connect via localhost and the mapped port.
.github/workflows/ci.yml
services:
redis:
image: redis:7
ports:
- 6379:6379
steps:
- run: redis-cli -h localhost -p 6379 pingUse the service name for container jobs
- If the job runs in a container, connect using the service name as the host.
- No host port mapping is needed in that case.
How to prevent it
- Declare ports when steps run on the runner host.
- Use the service name as host when the job runs in a container.
Related guides
GitHub Actions Service Container Port Not ReachableFix GitHub Actions service containers you cannot connect to - using localhost from a container job, or not ma…
GitHub Actions service container options health-cmd invalidFix a service container that never becomes healthy because the health-cmd passed through options is malformed.
GitHub Actions Container Job Fails to Start - Image, Entrypoint, or VolumeFix GitHub Actions container job failures - a missing image, registry auth, an entrypoint that exits, or volu…