簡體   English   中英

Docker Compose:Rails不會尊重DATABASE_URL連接到Postgres

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

我正試圖將我的Ruby on Rails 5.1.0應用程序容器化,但是我遇到了一些問題,因為它沒有從環境中獲取DATABASE_URL 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

如果我運行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"

但是如果我運行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!

知道我在這里做錯了嗎?

編輯:這是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

我有同樣的問題,似乎配置合並在Rails 5.1.x上以某種方式被破壞了

您可以通過在default塊中添加url: <%= ENV['DATABASE_URL'] %>來強制使用DATABASE_URL

我也有類似的問題,其原因是多方面的。

TL; DR,請確保:

  1. 您的app容器已正確鏈接到您的postgres容器並依賴於它
  2. 您的DATABASE_URL引用了postgres容器的正確主機名
  3. RAILS_ENV ENV變量在app容器中顯式設置

首先,我必須確保我的docker_compose.yml中的app容器鏈接到postgres容器並依賴於postgres容器:

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

第二,我提供的DATABASE_URL ENV變量database.yml沒有引用postgres容器的正確主機名。 最初,我看起來像:

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

我改成了:

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

在你的情況下不是一個明顯的問題,但值得注意的是其他人。

第三,我沒有明確指定RAILS_ENV環境。 我將ENV變量設置為docker_compose.yml development ,以便使用正確的database.yml配置。

我的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'] %>

我的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:

之后,所有bundle exec rake db:命令都運行得很好。

希望有所幫助!

問題出在rake任務中,它正在嘗試創建測試數據庫,我假設你的database.yml有一個帶有localhost主機的測試環境。

rake db:create的描述

從DATABASE_URL或config / database.yml為當前RAILS_ENV創建數據庫(使用db:create:all在配置中創建所有數據庫)。 沒有RAILS_ENV,它默認創建開發和測試數據庫

您應該明確地傳遞env,否則它可以默認創建一個測試數據庫。

我建議你改變測試數據庫credentionals在database.yml

Connection Preference

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM