skip to Main Content

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


  1. 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:

    • MySQL
    • PostgreSQL
    • Redis

    I tried the below gitlab-ci.yml with "postgres" image, it works:

    Ref: https://gitlab.com/gitlab-examples/postgres/-/blob/master/.gitlab-ci.yml

    password-auth:
      services:
        - postgres
      variables:
        # Configure postgres service (https://hub.docker.com/_/postgres/)
        POSTGRES_DB: custom_db
        POSTGRES_USER: custom_user
        POSTGRES_PASSWORD: custom_pass
      image: postgres
      script:
        # official way to provide password to psql: http://www.postgresql.org/docs/9.3/static/libpq-envars.html
        - export PGPASSWORD=$POSTGRES_PASSWORD
        - psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"
    
    host-auth:
      services:
        - postgres
      variables:
        # Configure postgres service (https://hub.docker.com/_/postgres/)
        POSTGRES_DB: custom_db
        POSTGRES_USER: custom_user
        POSTGRES_HOST_AUTH_METHOD: trust
      image: postgres
      script:
        - psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"
    

    Output:

    enter image description here

    Login or Signup to reply.
  2. 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.

    myjob:
      image: $CI_REGISTRY_IMAGE/ci_test
      services:
        - name: $CI_REGISTRY_IMAGE/ci_test
          alias: postgres
      script:
        - psql -h postgres -U postgres
    

    Alternatively, you can start the server in the script:

    myjob:
      # ...
      script:
        - 'docker-entrypoint.sh postgres &'  # start the server
        - sleep 60  # wait for it to start... this could be improved
        - psql -h localhost -U postgres
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search