简体   繁体   English

Docker无法在Travis CI中与Postgresql建立连接

[英]Docker cannot establish connection with postgresql in Travis CI

I'm trying to integrate Travis CI into my rails app. 我正在尝试将Travis CI集成到我的Rails应用程序中。 Everything works fine on my local machine(ubuntu, windows), but on travis host there is a connection error. 在我的本地计算机(ubuntu,Windows)上一切正常,但是在travis主机上存在连接错误。 So, here are my configurations. 所以,这是我的配置。

.travis.yml : .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 : database.yml

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

docker-compose.commands.yml : 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 但是travis日志在运行迁移时会输出此信息

Status: Downloaded newer image for postgres:9.6.2 状态:为Postgres下载的较新图像:9.6.2

Creating myapp_postgres_1 ... 创建myapp_postgres_1 ...

Creating myapp_postgres_1 创建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? 无法连接到服务器:连接被拒绝服务器是否在主机“ postgres”(172.18.0.2)上运行并接受端口5432上的TCP / IP连接? Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"my_app_development", "pool"=>5, "username"=>"postgres", "host"=>"postgres", "port"=>5432} rails aborted! 无法为{“ adapter” =>“ postgresql”,“ encoding” =>“ unicode”,“ database” =>“ my_app_development”,“ pool” => 5,“ username” =>“ postgres”创建数据库, “ host” =>“ postgres”,“ port” => 5432}导轨中断! 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? PG :: ConnectionBad:无法连接到服务器:连接被拒绝服务器是否在主机“ postgres”(172.18.0.2)上运行并接受端口5432上的TCP / IP连接?

Thanks to @johnharris85 and @jbielick I've found this solution: 感谢@ johnharris85和@jbielick,我找到了这个解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM