[dancer-users] Passing environment to dockerized Dancer2

Adam Clarke adamc at strategicdata.com.au
Tue Jul 20 08:52:48 EDT 2021

> On 19 Jul 2021, at 2:54 am, Mikko Johannes Koivunalho <mikko.koivunalho at iki.fi> wrote:
>> Problem:
>> How do I pass the environment dependent parts of the configuration to
>> the docker? I cannot use file `config_local.yaml` because then I would
>> need to copy that file somehow into the container when it starts.
>> Possible solution:
>> I would pass parts of the configuration as environment variables, e.g.
>> /DB_HOST/  and/SITE_URL/, and then apply these over the values in
>> config.yaml when I am creating the endpoints.
>> Suggestion:
>> Change the command `/config/` (or the underlying file reader function)
>> so that it can transpose environment variables into config strings, e.g.
>> plugins->database->connections->main->host = '${DB_HOST}'.
> I have built a solution to this.
> I have not yet submitted a Pull Request.
> I am using this solution in my build and web application.
> Please comment. Would this be a good pull request?

I’m not clear that this complexity is required give that the solution can be
achieved using a startup script as per Racke’s previous reply. Also I’m
assuming you’re not running your containers under Docker swarm or
Kubernetes but if you are there are more advanced ways to achieve what I
describe below.

In case Racke’s approach wasn’t clear, what you do is add an entry point
script to your Docker image build using the ENTRYPOINT instruction.
e.g. startup.sh. This script will always run at container start up.

The startup script would use the environment you had set to build an
appropriate config_local.yaml inside your container before Dancer is
invoked. There are lots of ways to do this, one simple and way from a
shell script is to use envsubst. The last thing the startup script would do
is start your Dancer app.

If you want or need your container to run in read only mode then you
can combine this technique with a tempfs mount for config_local.yaml.

* https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#entrypoint
* https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html

More information about the dancer-users mailing list