Skip to content

could not translate host name "postgres" to address: Name or service not known

For no apparent reason my CI builds started to fail when connecting to PosgreSQL. I'm not entirely sure how to container linking is supposed to work but adding echo /etc/host to .gitlab-ci.yml confirms that there is no corresponding entry for postgres. I retried the build several times with no change.

Basic information:

admin@yang:~$ gitlab-runner --version
Version:      9.4.2
Git revision: 6d06f2e
Git branch:   9-4-stable
GO version:   go1.8.3
Built:        Wed, 02 Aug 2017 12:46:17 +0000
OS/Arch:      linux/amd64

My .gitlab-ci.yml

  image: ruby:2.4
  services:
    - postgres:9-alpine

  variables:
    POSTGRES_USER: domore
    POSTGRES_PASSWORD: domore

  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - echo "${SSH_PRIVATE_KEY_BASE64}" |  tr -d ' ' | base64 --decode | ssh-add -
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host * \n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - cat /etc/hosts
    - gem install --no-document bundler
    - bundle install

  rubocop:
    stage: test
    script:
      - bundle exec rubocop

  rspec:
    stage: test
    script:
      - RAILS_ENV=test bundle exec rake db:create
      - RAILS_ENV=test bundle exec rake db:migrate
      - RAILS_ENV=test bundle exec rake spec

  staging:
    stage: deploy
    environment: staging
    only:
      - master
    script:
      - bundle exec cap staging deploy

Build log:

Running with gitlab-ci-multi-runner 9.4.2 (6d06f2e)
  on Alpine Linux 3.4 (c30fadd0)
Using Docker executor with image ruby:2.4 ...
Starting service postgres:9-alpine ...
Pulling docker image postgres:9-alpine ...
Using docker image postgres:9-alpine ID=sha256:e9e9c44705225c94547a659ce26e9b8c244de7aaf777b93c0e28a9012caa3ba0 for postgres service...
Waiting for services to be up and running...
Using docker image sha256:132a32cd825705548cfd1eda6a6887949dac16f55b540a04a9dd91718c3f016c for predefined container...
Pulling docker image ruby:2.4 ...
Using docker image ruby:2.4 ID=sha256:3630c02d3d1b80bb1413f80c164a06a1184577d22cb12e5479dd6ee081449b77 for build container...
Running on runner-c30fadd0-project-102-concurrent-0 via yang...
Fetching changes...
HEAD is now at 4288db7 Merge branch 'return-422-instead-of-401' into 'master'
Checking out 4288db78 as master...
Skipping Git submodules setup
$ which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
$ eval $(ssh-agent -s)
$ echo "${SSH_PRIVATE_KEY_BASE64}" |  tr -d ' ' | base64 --decode | ssh-add -
$ mkdir -p ~/.ssh
$ [[ -f /.dockerenv ]] && echo -e "Host * \n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
$ cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.12	runner-c30fadd0-project-102-concurrent-0
$ gem install --no-document bundler
$ bundle install
(...)
$ RAILS_ENV=test bundle exec rake db:create
could not translate host name "postgres" to address: Name or service not known
Couldn't create database for {"adapter"=>"postgresql", "host"=>"postgres", "port"=>5432, "pool"=>5, "timeout"=>5000, "user"=>"domore", "password"=>"domore", "database"=>"domore_test"}
rake aborted!
PG::ConnectionBad: could not translate host name "postgres" to address: Name or service not known
/usr/local/bundle/gems/pg-0.21.0/lib/pg.rb:56:in `initialize'
/usr/local/bundle/gems/pg-0.21.0/lib/pg.rb:56:in `new'
/usr/local/bundle/gems/pg-0.21.0/lib/pg.rb:56:in `connect'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/connection_adapters/postgresql_adapter.rb:701:in `connect'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/connection_adapters/postgresql_adapter.rb:220:in `initialize'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `new'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `postgresql_connection'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:759:in `new_connection'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:803:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:782:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:743:in `acquire_connection'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:500:in `checkout'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `connection'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/connection_handling.rb:116:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/connection_handling.rb:88:in `connection'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/tasks/postgresql_database_tasks.rb:10:in `connection'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/tasks/postgresql_database_tasks.rb:19:in `create'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/tasks/database_tasks.rb:117:in `create'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/tasks/database_tasks.rb:137:in `block in create_current'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/tasks/database_tasks.rb:304:in `block in each_current_configuration'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/tasks/database_tasks.rb:303:in `each'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/tasks/database_tasks.rb:303:in `each_current_configuration'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/tasks/database_tasks.rb:136:in `create_current'
/usr/local/bundle/gems/activerecord-5.1.2/lib/active_record/railties/databases.rake:27:in `block (2 levels) in <top (required)>'
/usr/local/bundle/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
/usr/local/bundle/bin/bundle:23:in `load'
/usr/local/bundle/bin/bundle:23:in `<main>'
Tasks: TOP => db:create
(See full trace by running task with --trace)
I, [2017-08-16T12:30:27.411985 #29509]  INFO -- sentry: ** [Raven] Raven 2.5.3 configured not to capture errors: Not configured to send/capture in environment 'test'
ERROR: Job failed: exit code 1