简体   繁体   English

Docker Compose:Rails不会尊重DATABASE_URL连接到Postgres

[英]Docker Compose: Rails won't respect the DATABASE_URL to connect to Postgres

I'm trying to containerize my Ruby on Rails 5.1.0 application, but I'm having some trouble with it not picking up DATABASE_URL from the environment. 我正试图将我的Ruby on Rails 5.1.0应用程序容器化,但是我遇到了一些问题,因为它没有从环境中获取DATABASE_URL In docker-compose.yml , I have the following service: docker-compose.yml ,我有以下服务:

app:
  build: .
  command: bundle exec rails s -p 3000 -b '0.0.0.0'
  volumes:
    - .:/myapp
  environment:
    DATABASE_URL: postgres://postgres:pw@db:5432/myapp_development

The environment gets picked up just fine if I run docker-compose run app rails c : 如果我运行docker-compose run app rails c ,环境就会好起来:

$ docker-compose run app rails c
Running via Spring preloader in process 25
Loading development environment (Rails 5.1.0)
irb(main):001:0> ENV['DATABASE_URL']
=> "postgres://postgres:pw@db:5432/myapp_development"

But then if I run docker-compose run app rake db:create , I get an error about not being able to connect to localhost : 但是如果我运行docker-compose run app rake db:create ,我会收到一条关于无法连接到localhost的错误:

$ docker-compose run app rake db:create
Database 'myapp_development' already exists
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?
Couldn't create database for {"adapter"=>"postgresql", "host"=>"localhost", "pool"=>10, "timeout"=>5000, "database"=>"myapp_test"}
rake aborted!

Any idea what I'm doing wrong here? 知道我在这里做错了吗?

Edit: Here's what database.yml looks like: 编辑:这是database.yml样子:

common: &common
  adapter: postgresql
  pool: 10
  timeout: 5000

development:
  <<: *common
  database: myapp_development

test:
  <<: *common
  database: myapp_test

production:
  <<: *common
  database: myapp_production

I had the same issue, it seems like the config merging is somehow broken on Rails 5.1.x 我有同样的问题,似乎配置合并在Rails 5.1.x上以某种方式被破坏了

You can force the use of DATABASE_URL by adding url: <%= ENV['DATABASE_URL'] %> in your default block. 您可以通过在default块中添加url: <%= ENV['DATABASE_URL'] %>来强制使用DATABASE_URL

I also had a similar issue, the causes of which were multi-fold. 我也有类似的问题,其原因是多方面的。

TL;DR, make sure that: TL; DR,请确保:

  1. Your app container is correctly linked to and dependent on your postgres container 您的app容器已正确链接到您的postgres容器并依赖于它
  2. Your DATABASE_URL references the correct hostname of your postgres container 您的DATABASE_URL引用了postgres容器的正确主机名
  3. The RAILS_ENV ENV variable is explicitly set within your app container RAILS_ENV ENV变量在app容器中显式设置

First off, I had to make sure my app container in docker_compose.yml was linked to and depended on the postgres container: 首先,我必须确保我的docker_compose.yml中的app容器链接到postgres容器并依赖于postgres容器:

version: '3'
    services:
      app:
        container_name: 'app'
        depends_on:
          - postgres
        links:
          - postgres

Second, the DATABASE_URL ENV variable I was providing database.yml did not reference the correct hostname of the postgres container. 第二,我提供的DATABASE_URL ENV变量database.yml没有引用postgres容器的正确主机名。 Originally, mine looked like: 最初,我看起来像:

'postgres://postgres:@localhost:5432/my_app_development'

I changed it to: 我改成了:

'postgres://postgres:@postgres:5432/my_app_development'

Not an apparent issue in your case, but worth noting for others. 在你的情况下不是一个明显的问题,但值得注意的是其他人。

Third, I wasn't explicitly specifying the RAILS_ENV environment. 第三,我没有明确指定RAILS_ENV环境。 I set that ENV variable to development in docker_compose.yml so that the correct database.yml configuration was used. 我将ENV变量设置为docker_compose.yml development ,以便使用正确的database.yml配置。

The end result of my database.yml file: 我的database.yml文件的最终结果:

development: &default
  adapter: postgresql
  encoding: unicode
  database: my_app_development
  username: postgres
  pool: 5
  timeout: 5000
  url: <%= ENV['DATABASE_URL'] %>

test:
  <<: *default
  database: my_app_test
  pool: 5
  url: <%= ENV['DATABASE_URL'] %>

production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

The end result of my docker_compose.yml file: 我的docker_compose.yml文件的最终结果:

version: '3'
services:
  app:
    container_name: 'app'
    environment:
      DATABASE_URL: 'postgres://postgres:@postgres:5432/my_app_development'
      RAILS_ENV: 'development'
    build: .
    ports:
      - '3000:3000'
    volumes:
      - ./:/dev
    depends_on:
      - postgres
    links:
      - postgres
    tty: true

  postgres:
    container_name: 'postgres'
    image: postgres:9.5
    ports:
      - '5432:5432'
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD:

After that, all bundle exec rake db: commands worked just fine. 之后,所有bundle exec rake db:命令都运行得很好。

Hope that helps! 希望有所帮助!

The problem is in the rake task, it is trying to create the test database, I assume your database.yml has a test environment with localhost host. 问题出在rake任务中,它正在尝试创建测试数据库,我假设你的database.yml有一个带有localhost主机的测试环境。

From the description of rake db:create rake db:create的描述

Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databases in the config). 从DATABASE_URL或config / database.yml为当前RAILS_ENV创建数据库(使用db:create:all在配置中创建所有数据库)。 Without RAILS_ENV it defaults to creating the development and test databases 没有RAILS_ENV,它默认创建开发和测试数据库

You should pass the env evidently or it could create a test database by default. 您应该明确地传递env,否则它可以默认创建一个测试数据库。

I suggest you to change the test database credentionals in the database.yml . 我建议你改变测试数据库credentionals在database.yml

Connection Preference

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM