laserlemon / figaro

Simple Rails app configuration
MIT License
3.77k stars 287 forks source link

Using with docker and ENV.fetch #269

Closed belgoros closed 6 years ago

belgoros commented 6 years ago

When reading Docker For Rails developers, I got an issue when the database secrets were defined in env files:

#.env/development/database

POSTGRES_USER=postgres
POSTGRES_PASSWORD=
POSTGRES_DB=myapp_development

In a docker-compose.yml this file is referenced:

...
env_file:
      - .env/development/database
      - .env/development/web
...

and its values are fetched in a Rails app database.yml file as follows:

default: &default
  adapter: postgresql
  encoding: unicode
  host: <%= ENV.fetch['DATABASE_HOST'] %>
  username: <%= ENV.fetch['POSTGRES_USER'] %>
  database: <%= ENV.fetch['POSTGRES_DB'] %>
  pool: 5
  variables:
    statement_timeout: 5000

development:
  <<: *default
...

It seems like it comes in conflict with the Figaro way to use ENV variable. If I drop fetch:

default: &default
  adapter: postgresql
  encoding: unicode
  host: <%= ENV['DATABASE_HOST'] %>
  username: <%= ENV['POSTGRES_USER'] %>
  database: <%= ENV['POSTGRES_DB'] %>
  pool: 5
  variables:
    statement_timeout: 5000

development:
  <<: *default
...

it works on macOS but fails on a Linux machine:

rails aborted!
ArgumentError: Cannot load `Rails.application.database_configuration`:
wrong number of arguments (given 0, expected 1..2)
(erb):4:in `fetch'
(erb):4:in `<main>'
...

NOTE: I have no values defined in my application.yml file for DATABASE_HOST, POSTGRES_USER, POSTGRES_DB keys.

So the question is what is right way to do that ? Thank you.

Figaro version: 1.1.1.

belgoros commented 6 years ago

My bad, I figured out what was the error, - wrong syntax:

ENV.fetch['DATABASE_HOST']

should be

ENV.fetch('DATABASE_HOST', 'some_defaut_value_if_not_found')