I’ve created a very simple custom Docker image:
FROM postgis/postgis:16-3.4-alpine
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=secret
ENV POSTGRES_DB=lopost_test_db
ENV PGDATA=/data/postgres
ENV POSTGRES_HOST_AUTH_METHOD=trust
Now I’m trying to run the following gitlab job
test:
image: $CI_REGISTRY_IMAGE/ci_test
stage: test
environment:
name: Production
script:
- psql -h localhost -U postgres
When the job runs unfortunately I get the following output:
$ psql -h localhost -U postgres
psql: error: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
I am able to run the same setup on my local environment and it works perfectly fine. I am on Windows/WSL, I tried to look if host resolution works differently but I couldn’t find anything that helped with the issue.
Thanks in advance for any help
2
Answers
You can specify an additional image by using the services keyword. This additional image is used to create another container, which is available to the first container. The two containers have access to one another and can communicate when running the job.
The service image can run any application, but the most common use case is to run a database container, for example:
I tried the below gitlab-ci.yml with "postgres" image, it works:
Ref: https://gitlab.com/gitlab-examples/postgres/-/blob/master/.gitlab-ci.yml
Output:
You haven’t started the database server, so that’s why you cannot connect.
The typical way to do this is to use
services:
in GitLab. This has the benefit that your job will wait for the service to be available before the job script starts.Alternatively, you can start the server in the script: