Sub topic 12
Debugging a service that will not start or connect
This chapter assumes you can write a services block, know the difference between a job on the runner and a job in a container, and have used health checks. It covers how to find out why a service failed and how to fix the usual causes.
Where the evidence is
Service containers are started during the "Initialize containers" step, before your first step runs. Open that step in the job log first. It shows the image pull, the container creation, and the health check progress. If the service never became healthy, the job fails here, and your test steps never run.
If the job gets past this step but tests cannot connect, the problem is usually the address, the port, or the service's own startup. Add a diagnostic step before your tests:
- name: Show service state
if: always()
run: |
docker ps --all
docker logs "$(docker ps --all --filter ancestor=postgres:16 --quiet | head -n 1)"if: always() makes the step run even when an earlier step failed, which is when you need the logs. This works for jobs that run on the runner, where the Docker CLI is available.
Symptoms and causes
| Symptom in the log | Likely cause | Fix |
|---|---|---|
| `connection refused` on `localhost` | Job runs in a container, so `localhost` is the job container, not the service | Use the service label (`postgres`) as the host |
| `could not translate host name "postgres"` | Job runs on the runner, so the label does not resolve | Use `localhost` and a mapped port |
| `connection refused` on the right host | Service is still starting, or no health check is set | Add `--health-cmd` and related options |
| Job fails in "Initialize containers" with an unhealthy status | Health command is wrong, or the service crashed | Read the service logs and test the command locally |
| Port is empty in the connection string | `job.services.<label>.ports` used with a fixed mapping, or the wrong label or port key | Check the label and use the container port as the key |
| Authentication failed | Credentials in `env` differ from those in the URL | Make both use the same values |
Work through it in order
- Confirm the networking mode. Look for a
container:key on the job. If present, use the label as the host and the container's own port. If absent, uselocalhostand the host port fromports. - Confirm the port. With a fixed mapping such as
5432:5432, the host port is 5432. With a dynamic mapping such as5432/tcp, read the port fromjob.services.<label>.ports['5432']. - Confirm the health check. The command runs inside the service container, so it must exist in that image.
pg_isreadyworks in the PostgreSQL image. A command that is missing marks the service unhealthy forever. - Confirm the environment. Images often refuse to start without a required variable. The official PostgreSQL image exits at once if
POSTGRES_PASSWORDis not set. - Confirm the image tag. A typo or a tag that does not exist fails at the pull stage.
Print the settings your tests receive
Many failures come from a connection string that is not what you think. Print it with the password masked by the app, or print only the host and port:
- name: Check connection target
run: |
echo "Host and port: ${DATABASE_URL#*@}"
nc -zv localhost 5432The shell expansion ${DATABASE_URL#*@} removes everything up to the @, so the password is not printed. nc -zv tests whether the port accepts a connection, and it separates a networking problem from an application problem. In a container job, replace localhost with the service label, and make sure nc is installed in that image.
Exercise
Your job has container: node:20 and a postgres service. Tests fail with connection refused at localhost:5432, but the Initialize containers log shows the service is healthy. What is wrong, and what is the fix?
Show answer
Because the job runs in a container, localhost refers to the job container, not the service. Change the host in the connection string to the service label, postgres, and keep port 5432. A ports mapping is not needed.