【问题标题】:Docker - Rails app cannot connect to linked Postgres container (doesn't seem to be running)Docker - Rails 应用程序无法连接到链接的 Postgres 容器(似乎没有运行)
【发布时间】:2017-02-13 03:49:08
【问题描述】:

我一直在关注如何使用 Docker 设置 Rails 开发环境的指南:Setting Up A Rails Development Environment Using Docker。

在此过程中我遇到了一些障碍,但在运行 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?

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

【问题讨论】:

    标签: postgresql docker docker-compose


    【解决方案1】:

    您尝试连接到localhost:5432,换句话说,您尝试连接到web 容器,但您的意思是db。只需在您的应用程序中指定数据库主机,例如 db

    【讨论】:

    • 感谢您的回复。你是说它应该是我的 Rails database.yml 文件中的db:5432(指定要连接到哪个数据库的文件)?
    • 是的,完全正确。请尝试这个解决方案,让我知道会发生什么。
    • 我刚刚在我自己的 Dockerized Rails 应用程序上尝试了这个提示,它确实有效。在 Docker 中连接时,database.yml 中的主机应该是包含 db 的容器名称,而不是 localhost。当然,这取决于您的配置。
    猜你喜欢
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    • 2019-08-11
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2017-11-03
    相关资源
    最近更新 更多