gpt4 book ai didi

ruby-on-rails - Docker无法在Travis CI中与Postgresql建立连接

转载 作者:行者123 更新时间:2023-12-02 20:15:12 27 4
gpt4 key购买 nike

我正在尝试将Travis CI集成到我的Rails应用程序中。在我的本地计算机(ubuntu,Windows)上一切正常,但是在travis主机上存在连接错误。所以,这是我的配置。
.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:

但是travis日志在运行迁移时输出

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?

最佳答案

感谢@ 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:
#!/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

关于ruby-on-rails - Docker无法在Travis CI中与Postgresql建立连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43896250/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com