gitlab ci error could not translate host name "postgres" to address: Name does not resolve
Asked Answered
D

3

13

I use gitlab-ci in my rails app, it ran correctly till yesterday but it does not pass due to:

rake aborted!
PG::ConnectionBad: could not translate host name "postgres" to address: Name does not resolve
/usr/local/bundle/gems/pg-1.1.4/lib/pg.rb:56:in `initialize'
/usr/local/bundle/gems/pg-1.1.4/lib/pg.rb:56:in `new'
/usr/local/bundle/gems/pg-1.1.4/lib/pg.rb:56:in `connect'
....
Tasks: TOP => db:schema:load => db:check_protected_environments

.gitlab-ci.yml :

rspec:
  stage: test
  services:
    - postgres:10
  variables:
    DATABASE_URL: "postgresql://postgres:postgres@postgres:5432/$POSTGRES_DB"
    POSTGRES_DB: db_test
    RAILS_ENV: test
  before_script:
    - ruby -v
  script:
    - cp config/application.sample.yml config/application.yml
    - cp config/database.sample.yml config/database.yml
    - bundle exec rake db:schema:load
    - bundle exec rspec spec

It seems it can not find the postgres service running or for some reason the database service is not running correctly, I guess some internals has changed in gitlab-ci.

Derive answered 15/2, 2020 at 9:12 Comment(1)
Experiencing same issue with django app as well. Definitely something internal changed.Hong
C
13

EDIT: This was an intended change to the images, you now must set a password or configure further:

If you know that you want to be insecure (i.e. any one can connect without a password from anywhere), then POSTGRES_HOST_AUTH_METHOD=trust is how you opt in to that.


This seems to have been introduced when the docker images were upgraded to the new releases.

You can pull the 10.11 image instead to avoid this problem for the time being:

services:
- postgres:10.11

Not sure why this is happening, but we are experiencing the same since the last docker image update. I have found this to also be the case going from 12.1 to 12.2.

Caine answered 15/2, 2020 at 19:12 Comment(0)
B
4

postgres has two required environment variables names POSTGRES_USER and POSTGRES_PASSWORD if you not provide them container will not run.

gitlab-ci documentation about services

Brott answered 15/2, 2020 at 13:23 Comment(0)
T
0

Also have same problem, but my case was that I have started new docker inside script block:

test:
  stage: test
  image: ${GOOGLE_CLOUD_IMAGE}
  only:
    - merge_requests
  extends: .build-backend
  services:
    - docker:19.03.5-dind
    - postgres:11.1-alpine
  script:
    - docker build -t ${IMAGE_NAME}:${CI_COMMIT_SHA} -f docker/Dockerfile.prod .
    - docker run -t sh -c 'poetry run python manage.py test apps'

Problem was that postgres alias was create into script space, not into new docker container space, so I started using postgres inside script and have connected to local network:

test:
  stage: test
  image: ${GOOGLE_CLOUD_IMAGE}
  only:
    - merge_requests
  extends: .build-backend
  services:
    - docker:19.03.5-dind
  script:
    - docker network create my-net
    - docker run -d --network my-net --name postgres postgres:11.1-alpine 
    - docker build -t ${IMAGE_NAME}:${CI_COMMIT_SHA} -f docker/Dockerfile.prod .
    - docker run -t --network my-net sh -c 'poetry run python manage.py test apps'
Thordia answered 10/2, 2023 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.