Docker - Rails 应用程序无法连接到链接的 Postgres 容器(似乎没有运行) [英] Docker - Rails app cannot connect to linked Postgres container (doesn't seem to be running)

查看:28
本文介绍了Docker - Rails 应用程序无法连接到链接的 Postgres 容器(似乎没有运行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been following this guide of how to set up a Rails Development Environment using Docker: Setting Up A Rails Development Environment Using Docker.

在此过程中我遇到了一些障碍,但在运行 Rails 迁移步骤之前,我已经设法解决了其中的大部分问题.运行命令 docker-compose run web rake db:migrate 产生以下结果:

I've hit a few snags along the way, but I've managed to get through most of them up until the step of running Rails migration. Running the command docker-compose run web rake db:migrate yields the following result:

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

推荐答案

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

You try to connect to localhost:5432 in other words you try to connect to the web container, but your point is db. Just specify Database host in your application like db

这篇关于Docker - Rails 应用程序无法连接到链接的 Postgres 容器(似乎没有运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆