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?
depends_on
does not wait for postgres to be ready, just for the container to start. Look at using something likewait-for-it.sh
to wait for postgres to be up before connecting to it. – johnharris85