Friday, January 29, 2016

12 Factor Config for .Net on Mono in a Docker

Recently I've been thinking about how to run a .Net application using Mono inside a Docker, along with this we'd like to use Twelve Factor config.

Whilst it is possible to use some library to access environment variables instead of using appSettings it becomes difficult for database and message broker URLs which other libraries may expect to find within the App.config or Web.config file.

The solution I came up with follows:

Build the application without an App.config moving the existing configuration file renaming it App.config.template

Substitute any environment variables you want with ${}.  E.g:

<amqp> <connections> <connection name="rabbit" server="${RABBITMQ_URL}" password="${RABBITMQ_PASSWORD}" /> </connections> </amqp>

Include within the Docker image a script named sub.sh similar to this and add it to /conf:

#!/bin/sh
while read line
do
    eval echo "$line"
done
Add your config template to /conf

Then in the DockerFile, if your CMD is:

CMD ["mono", "/app/foo.exe" ]
Change it to:

CMD /conf/sub.sh /conf/App.config.template > /app/App.config; mono /app/foo.exe
Now when you run your Docker image, pass in your environment variables as usual. In this case, I'd set the RABBITMQ_URL and the RABBITMQ_PASSWORD

You can also configure these in a file and pass it into the docker run command using --env-file

1 comment:

Stuart said...

If you use Monodevelop, or Xamarin Studio, there is a remote debugger add-in called SSH Debugger. You can find this in the built in add-in repo.

With this, you can automatically deploy, and debug your .NET apps just by setting the IP of the host (or container) You may need change the script a bit for docker if you don't have ssh inside a container.

I plan to do a video on it soon.

Project page here:https://github.com/logicethos/SSHDebugger