Jack M. Jack M. - 1 year ago 41
Perl Question

Configure Dancer from environment variables?

I'm new to Dancer, but I'm trying to configure it to work within a Docker container. As a result, I need to pick up my database settings from the environment.

In my case, I have

, and
coming from Docker. Unfortunately, the
module is erroring before I can change the database to use those variables.

use Dancer ':syntax';
use Dancer::Plugin::Database;

if ($ENV{DB_PORT_3306_TCP}) {## Connected via docker.
driver => 'mysql',
username => 'username',
password => 'password',
host => $ENV{DB_PORT_3306_TCP_ADDR},
port => $ENV{DB_PORT_3306_TCP_PORT},

So the question is, is there a good way to configure Dancer from environment variables, instead of through static YAML?

Answer Source

It turns out there is actually a configuration shortcut for accessing the environment. Within your config.yml, __ENV()__ will be replaced with matching environment variables. For example:

    driver: mysql
    host: __ENV(DB_PORT_3306_TCP_ADDR)__
    port: __ENV(DB_PORT_3306_TCP_PORT)__
    database: __ENV(DB_DATABASE)__
    user: __ENV(DB_USERNAME)__
    password: __ENV(DB_PASSWORD)__