0

I have a simple PHP app which keeps its configuration in a JSON file which is loaded into the app using the Noodlehouse\Config configuration loader. Now I would like to share this app also as a Docker image, but I don’t want the configuration to be embedded in the image. As I have read - the preferred Docker-way would be passing these configuration variables as environment variables.

What is the best way to do this? Do I have to rewrite the configuration loading part in my app so it’s able to load config variables from the environment or are there any nice tricks so I don’t have to overcomplicate the configuration loading process in my app?

I don’t want to have the config loaded only via env variables because some instances of this app are being used on a shared server.

I don’t want my app to be “Docker-only” - the docker image is just in case as a way to run the app.

My example configuration for this app:

{
    "provider": {
        "api": {
            "hash": "9061b79dc966a3fe0831dec",
            "login": "johndoe",
            "pass": "7MAW8S!Dc2aT6Lg*i&rkpF5k!P"
        }
    },
    "mailer": {
        "host": "smtp.contoso.com",
        "port": 587,
        "user": "noreply@contoso.com",
        "password": "3k@2veWF9i&SKESjv9g83!o*v@",
    }
}

The way I’m loading the configuration:

use Noodlehaus\Config;
$config = Config::load('config/config.json');

echo $config->get('provider.api.hash');
echo $config->get('provider.api.login');
echo $config->get('provider.api.pass');

Is there a nice way of doing this? Maybe I should use another configuration loader?

Thomas Szteliga
  • 402
  • 4
  • 15

1 Answers1

0

OK, after seeing my question here I checked the Noodlehaus\Config better, because there should be an option to provide a default value and there is the second argument for get designed for this purpouse.

I guess this way is acceptable and will not overcomplicate my configuration loading process:

use Noodlehaus\Config;
$config = Config::load('config/config.json');

echo $config->get('provider.api.hash', getenv('MYAPP_PROVIDER_API_HASH'));
echo $config->get('provider.api.login', getenv('MYAPP_PROVIDER_API_LOGIN'));
echo $config->get('provider.api.pass', getenv('MYAPP_PROVIDER_API_PASS'));

And I'll simply keep my config.json empty in the Docker image so $config->get(...) will fall back to the second argument.

Thomas Szteliga
  • 402
  • 4
  • 15