1
votes

I'm trying to integrate Travis CI into my rails app. Everything works fine on my local machine(ubuntu, windows), but on travis host there is a connection error. So, here are my configurations.

.travis.yml:

sudo: required

language: ruby

services:
  - docker

env:
  DOCKER_COMPOSE_VERSION: 1.13.0

before_install:
  - sudo rm /usr/local/bin/docker-compose
  - curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
  - chmod +x docker-compose
  - sudo mv docker-compose /usr/local/bin
  - sudo apt-get update
  - sudo apt-get -y -o Dpkg::Options::="--force-confnew" install docker-engine
  - docker-compose build
  - docker-compose -f docker-compose.commands.yml run --rm website rails db:create db:migrate db:test:prepare

script:
 - docker-compose -f docker-compose.commands.yml run --rm -e RAILS_ENV=test website rspec

database.yml:

development: &default
adapter: postgresql
database: my_app_development
username: postgres
host: postgres
port: 5432

docker-compose.commands.yml:

version: '3'

services:
  postgres:
    image: 'postgres:9.6.2'
    ports:
      - "5432"

  website:
    depends_on:
      - 'postgres'
    build: .
    ports:
      - '3000'
  volumes:
    - '.:/my_app'
    - 'bundle_data:/bundle'

 volumes:
  bundle_data:

But travis log outputs this when it runs migrations

Status: Downloaded newer image for postgres:9.6.2

Creating myapp_postgres_1 ...

Creating myapp_postgres_1

could not connect to server: Connection refused Is the server running on host "postgres" (172.18.0.2) and accepting TCP/IP connections on port 5432? Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"my_app_development", "pool"=>5, "username"=>"postgres", "host"=>"postgres", "port"=>5432} rails aborted! PG::ConnectionBad: could not connect to server: Connection refused Is the server running on host "postgres" (172.18.0.2) and accepting TCP/IP connections on port 5432?

1
Likely a timing issue. depends_on does not wait for postgres to be ready, just for the container to start. Look at using something like wait-for-it.sh to wait for postgres to be up before connecting to it.johnharris85
I don't think so, please see updated log output descriptionSlava Nikulin
I don't see anything in the extra log that changes my opinion?johnharris85
the error occures after database container was created. Doesn't that mean that database is ready?Slava Nikulin
No, like I said, container created != postgres ready to accept connections.johnharris85

1 Answers

0
votes

Thanks to @johnharris85 and @jbielick I've found this solution:

website:
    depends_on:
      - 'postgres'
    build: .
    ports:
      - '3000'
    volumes:
      - '.:/my_app'
      - 'bundle_data:/bundle'
    entrypoint: ./wait-for-postgres.sh postgres 5432

  postgres:
    image: 'postgres:9.6.2'
    ports:
      - '5432'

wait-for-postgres.sh:

#!/bin/sh

postgres_host=$1
postgres_port=$2
shift 2
cmd="$@"

# wait for the postgres docker to be running
while ! pg_isready -h $postgres_host -p $postgres_port -q -U postgres; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

>&2 echo "Postgres is up - executing command"

# run the command
exec $cmd