0

My goal is to use environment variables in the docker-compose.yml file that can be referenced inside the application itself like in config/database.yml

docker-compose.yml


...
environment:
  $DB_NAME: myapplication
  $DB_USER: appadmin
  $DB_PASS: secret
...

config/database.yml

...
database: $DB_NAME
db_user: $DB_USER
db:pass: $DB_PASS
...

But no matter how do I try to send the variables to my file, They don't get recognised. :(

Bert
  • 1,028
  • 1
  • 16
  • 33
  • Does the application parsing config/database.yml inside the container expand environment variables defined in that file? – BMitch Jan 28 '20 at 20:06

1 Answers1

0

You're just trying to set the variables in the compose file? You shouldn't need the "$" in front of them in that case:

environment:
  - DB_NAME=database
  - DB_USER=appadmin
  - DB_PASS=secret

You could use mappings too:

environment:
  DB_NAME: database
  DB_USER: appadmin
  DB_PASS: secret

When you reference them in the database config file, you can just use the variable name too, again, without the "$":

environment:
  DB_NAME
  DB_USER
  DB_PASS

You can check the Compose Environment Options documentation and the Compose Environment reference for further information.

Hope that helps.

eramnes
  • 11
  • 1
  • I'm trying to set the variable values in the compose file, but then trying to call them inside the docker images database config file. Gonna try soon the non-$ type of calling it, but honestly, I don't know if that could work. I mean isn't the whole point of $ to let the system know that is a variable and not a string? – Bert Feb 03 '20 at 12:56