Rails: Use environment variables defined with Figaro in Docker

645 views Asked by At

If I'm not wrong, it seems like defined with Figaro variables are not available in Docker container. I have env files to configure Postgresq DB: POSTGRES_USER=ENV['db_user'] POSTGRES_PASSWORD=ENV['db_password] POSTGRES_DB=ENV['db_name'] I have application.yml file copied with all the other Rails app files to the container (I could check it with ls in the container shell).

Is it a normal behaviour ?

1

There are 1 answers

0
Promise Preston On

I also faced this issue when working on a Rails application.

The thing to note here is that for Docker, environment variables are placed in a .env file or an env_file (my_variables.env). Environment files placed in .yml or .yaml files (application.yml) are not often recognized by Docker during runtime. Here's a link to Docker's official documentation that further explains it: The “.env” file

An example of such environment variables are Database connection strings which are required during the application startup process like:

database_name
database_username
database_password

However, you can still use the Figaro gem for defining variables that are not required during application startup, like:

RAILS_MASTER_KEY

That's all.

I hope this helps