2017-05-19 33 views
2

我想containerize我的Ruby on Rails的5.1.0應用程序,但我有一些麻煩,它沒有從環境拿起DATABASE_URL。在docker-compose.yml,我有以下服務:碼頭工人撰寫:Rails會不會尊重DATABASE_URL連接到Postgres的

app: 
    build: . 
    command: bundle exec rails s -p 3000 -b '0.0.0.0' 
    volumes: 
    - .:/myapp 
    environment: 
    DATABASE_URL: postgres://postgres:[email protected]: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:[email protected]: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 
+0

我可以與你分享我的解決方案,但它不直接回答你的問題,所以我把它置於評論中。你可以添加獨立的容器到postgres,添加主機配置到你的database.yml匹配postgres容器名稱。然後登錄容器,你將能夠運行任何你想要的命令,沒有錯誤。如果你有興趣,我可以提供詳細的答案。 –

回答

0

的問題是在rake任務,它試圖創建測試數據庫,我假設你database.ymllocalhost主機測試環境。

rake db:create

的描述創建一個從DATABASE_URL或配置/ database.yml中爲 當前RAILS_ENV(使用分貝:創建:所有在 配置創建所有數據庫)中的數據庫。 沒有RAILS_ENV它默認爲創造發展和 測試數據庫

你顯然應該通過包膜它也可以創建默認測試數據庫。

我建議你更改database.yml中的測試數據庫信息。

Connection Preference

+0

這是有道理的,但我不明白爲什麼它告訴我'myapp_development'已經存在,然後在嘗試創建'myapp_test'時不會連接 - 在'database.yml'中它們連接到同一個地方。我將編輯我的答案以包含'database.yml'。 –

+0

我沒有收到您的評論。更改database.yml並在兩種環境中設置主機_evidently_。 –

+0

但是,如果我在環境中提供'DATABASE_URL',是不是不需要設置主機? –

1

我有同樣的問題,這似乎是在配置合併是on Rails的5.1.x中

您可以強制使用DATABASE_URL通過在default塊添加url: <%= ENV['DATABASE_URL'] %>某種程度上打破。

0

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

TL; DR,確保:

  1. app盒是否正確連接到並依賴於你的postgres容器
  2. DATABASE_URL引用您的postgres容器的正確的主機名
  3. RAILS_ENV ENV變量明確設置在您的app集裝箱內

首先,我必須確保我在docker_compose.ymlapp容器是有聯繫的,並依賴該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環境。我在docker_compose.yml中將ENV變量設置爲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:命令工作就好了。

希望有幫助!

相關問題