Docker - Rails应用程序无法连接到链接的Postgres容器(似乎没有运行)

Joh*_*ohn 5 postgresql docker docker-compose

我一直在关注如何使用Docker 设置 Rails开发环境的指南:使用Docker 设置Rails开发环境.

我一路上遇到了一些障碍,但是我已经成功地完成了大部分工作,直到运行Rails迁移的步骤.运行该命令会docker-compose run web rake db:migrate产生以下结果:

rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
Run Code Online (Sandbox Code Playgroud)

泊坞窗,compose.yml:

version: '2'
services:
  db:
    image: postgres
    volumes:
      - ./pgdata:/pgdata
    environment:
      POSTGRES_DB: myapp_development
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD:
      PGDATA: /pgdata
  web:
    build: .
    command: bundle exec rails server --port 5000 --binding 0.0.0.0
    volumes_from:
      - container:myapp-web-sync:rw
      - container:myapp-bundle-sync:rw
    volumes:
      - ./keys:/root/.ssh/
    ports:
      - "5000:5000"
    environment:
      REDIS_URL: redis://redis:6379
      GEM_HOME: /bundle
    links:
      - db
      - redis
volumes:
  myapp-web-sync:
    external: true
  myapp-bundle-sync:
    external: true
Run Code Online (Sandbox Code Playgroud)

Buk*_*gey 10

您尝试连接到localhost:5432换句话说,您尝试连接到web容器,但您的观点是db.只需在应用程序中指定数据库主机即可db

  • 感谢您的回复。你是说在我的 Rails `database.yml` 文件(指定要连接到哪个数据库的文件)中它应该是 `db:5432`? (4认同)
  • 对,就是这样。请尝试此解决方案,让我知道会发生什么。 (2认同)