Ruby Docker Postgres容器未启动Rails应用程序

Ruby Docker Postgres容器未启动Rails应用程序,ruby,docker,ruby-on-rails-4,docker-compose,Ruby,Docker,Ruby On Rails 4,Docker Compose,我面临一个关于docker容器的问题。当我执行docker compose up启动应用程序时,Postgres容器没有启动 我在docker撰写后得到的错误 /usr/local/bundle/gems/activerecord-4.2.0/lib/active\u record/connection\u adapters/postgresql\u adapter.rb:651:“初始化”中:无法将主机名“db”转换为地址:名称或服务未知(PG::ConnectionBad) 现在它经常发生。

我面临一个关于docker容器的问题。当我执行
docker compose up
启动应用程序时,Postgres容器没有启动

我在docker撰写后得到的错误

/usr/local/bundle/gems/activerecord-4.2.0/lib/active\u record/connection\u adapters/postgresql\u adapter.rb:651:“初始化”中:无法将主机名“db”转换为地址:名称或服务未知(PG::ConnectionBad)

现在它经常发生。我尝试了几个步骤,例如为
db
容器添加端口,即
5432:5432
。我曾经启动或停止特定的db容器,以便重新建立连接,但它不起作用

申请详情:

Rails版本:4.2.0 Ruby版本:2.2.0

docker compose.yml

version: '3.7'

services:
  selenium:
    image: selenium/standalone-chrome-debug:3.141.59-krypton
    ports: ['4444:4444', '5900:5900']
    logging:
      driver: none
  redis:
    image: redis:3.0.0
  elastic:
    image: elasticsearch:1.5.2
  db:
    image: postgres:9.3.10
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
      - .:/home
  XYZ:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    stdin_open: true
    tty: true
    volumes:
      - XYZ-sync:/home:nocopy
    ports:
      - "3000:3000"
    depends_on:
      - db
      - redis
      - elastic
      - selenium
    environment:
      - REDIS_URL=redis://redis:6379/0
      - ELASTICSEARCH_URL=elastic://elastic:9200/0
      - SELENIUM_HOST=selenium
      - SELENIUM_PORT=4444
      - TEST_APP_HOST=XYZ
      - TEST_PORT=3000
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: postgres
  password:
  host: db

development:
  <<: *default
  database: XYZ_development

test:
  <<: *default
  database: XYZ_test

development_migrate:
  adapter: mysql2
  encoding: utf8
  database: xyz_ee
  username: root
  password:
  host: localhost
  pool: 5
数据库日志

db_1          | LOG:  database system was shut down at 2019-09-10 07:37:08 UTC
db_1          | LOG:  MultiXact member wraparound protections are now enabled
db_1          | LOG:  database system is ready to accept connections
db_1          | LOG:  autovacuum launcher started
db_1          | LOG:  received smart shutdown request
db_1          | LOG:  autovacuum launcher shutting down
db_1          | LOG:  shutting down
db_1          | LOG:  database system is shut down
db_1          | LOG:  database system was shut down at 2019-09-10 07:37:50 UTC
db_1          | LOG:  MultiXact member wraparound protections are now enabled
db_1          | LOG:  database system is ready to accept connections
db_1          | LOG:  autovacuum launcher started
db_1          | LOG:  database system was interrupted; last known up at 2019-09-10 07:38:31 UTC
db_1          | LOG:  received smart shutdown request
db_1          | LOG:  database system was interrupted; last known up at 2019-09-10 07:38:31 UTC
db_1          | LOG:  database system was not properly shut down; automatic recovery in progress
db_1          | LOG:  record with zero length at 0/1D8F0120
db_1          | LOG:  redo is not required
db_1          | LOG:  MultiXact member wraparound protections are now enabled
db_1          | LOG:  autovacuum launcher started
db_1          | LOG:  database system is ready to accept connections
db_1          | LOG:  stats_timestamp 2019-09-10 08:02:39.288642+00 is later than collector's time 2019-09-10 08:02:39.189551+00 for database 0
db_1          | LOG:  database system was interrupted; last known up at 2019-09-10 08:18:02 UTC
db_1          | FATAL:  the database system is starting up
docker compose ps
输出

xyz_db_1           /docker-entrypoint.sh postgres   Up       5432/tcp                                      
xyz_elastic_1      /docker-entrypoint.sh elas ...   Up       9200/tcp, 9300/tcp                            
xyz_xyz_1   bash -c rm -f tmp/pids/ser ...   Exit 1                                                 
xyz_redis_1        /entrypoint.sh redis-server      Up       6379/tcp                                      
xyz_selenium_1     /opt/bin/entry_point.sh          Up       0.0.0.0:4444->4444/tcp, 0.0.0.0:5900->5900/tcp
database.yml

version: '3.7'

services:
  selenium:
    image: selenium/standalone-chrome-debug:3.141.59-krypton
    ports: ['4444:4444', '5900:5900']
    logging:
      driver: none
  redis:
    image: redis:3.0.0
  elastic:
    image: elasticsearch:1.5.2
  db:
    image: postgres:9.3.10
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
      - .:/home
  XYZ:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    stdin_open: true
    tty: true
    volumes:
      - XYZ-sync:/home:nocopy
    ports:
      - "3000:3000"
    depends_on:
      - db
      - redis
      - elastic
      - selenium
    environment:
      - REDIS_URL=redis://redis:6379/0
      - ELASTICSEARCH_URL=elastic://elastic:9200/0
      - SELENIUM_HOST=selenium
      - SELENIUM_PORT=4444
      - TEST_APP_HOST=XYZ
      - TEST_PORT=3000
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: postgres
  password:
  host: db

development:
  <<: *default
  database: XYZ_development

test:
  <<: *default
  database: XYZ_test

development_migrate:
  adapter: mysql2
  encoding: utf8
  database: xyz_ee
  username: root
  password:
  host: localhost
  pool: 5
default:&default
适配器:postgresql
编码:unicode
游泳池:5
用户名:postgres
密码:
主持人:db
发展:

我在@jayDosrsey建议的帮助下解决了我的问题。 DB容器在主web容器之前启动,因此它总是无法启动容器,我需要再次重新启动web容器

通过在启动rails服务器时添加运行状况检查条件,解决了此问题

XYZ:
  build: .
  command: bash -c "while !</dev/tcp/db/5432; do sleep 1; done; rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"

  ...

Now I am able to start the container in sequence.
XYZ:
生成:。

命令:bash-c“一会儿!您需要在compose中公开端口5432:5432。是否可以提供来自
docker compose logs db
的日志以及
docker compose ps
的日志helpful@JakubBujny我更新了问题,了解您的详细要求。您能分享您的
config/database.yml
吗?顺便说一句,您不需要公开端口5432,因为docker compose网络中的容器已经彼此可用。如果要从主机访问某些内容,只需公开端口