Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enviroment variables from docker-compose.yml have no effect #172

Open
1-alex98 opened this issue Aug 8, 2020 · 3 comments
Open

Enviroment variables from docker-compose.yml have no effect #172

1-alex98 opened this issue Aug 8, 2020 · 3 comments

Comments

@1-alex98
Copy link
Contributor

1-alex98 commented Aug 8, 2020

The DB options in the docker-compose file seem to have no effect on the connection parameters of the application instead only the config/database.yml is respected.

How would this be corrected? Like this?
puma.rb:

config_hash = YAML.load_file("#{app_dir}/config/database.yml")[rails_env]
if ENV["DB_USERNAME"]
      config_hash[:database] = ENV["DB_USERNAME"]
end
ActiveRecord::Base.establish_connection(
        config_hash)
@1-alex98 1-alex98 changed the title Docker-compose yml db settings Enviroment variables from ocker-compose.yml have no effect Aug 8, 2020
@s-edwards
Copy link
Member

Good catch--I agree you're correct. The code you suggest is one way to fix it. However, there are multiple settings in the app that may need to be overridden, including basically everything in the database.yml and secrets.yml files.

A better solution may be to use an erb expression in the yaml instead, so that any value can be conditionally overridden using ENV settings. For example:

development:
   adapter: mysql2
   database: <%= ENV["DB_DATABASE"] || 'codeworkout' %>
   username: <%= ENV["DB_USERNAME"] || 'codeworkout' %>
   password: <%= ENV["DB_PASSWORD"] || 'codeworkout' %>
   host: <%= ENV["DB_HOST"] || 'localhost' %>

This is supported in database.yml ... but would need to check on secrets.yml. With this change, the code itself would be unchanged and all the ENV support would go through the config file(s).

@1-alex98
Copy link
Contributor Author

So it needs to be renamed .yml.erb right?

@s-edwards
Copy link
Member

No, ERB notation is supported directly in the file and the extension does not have to be changed. (From docs, but not tested :-).)

@ayaankazerouni ayaankazerouni changed the title Enviroment variables from ocker-compose.yml have no effect Enviroment variables from docker-compose.yml have no effect Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants