Configuration with Environment Variables – With Visual Studio Docker Tools

I posted recently about using environment variables to configuration. I like to use this approach with Docker, too!

Visual Studio has some great tool support (in preview) for working with Docker. With the release of Windows Server 2016 Technical Preview 3, there is now a preview of Windows Server Containers. ScottGu has a great post about this that gives an overview of the update to the VS Tools as well, so go check that out Smile

In the new release of the Docker Tools for VS, as well as adding support for Windows Server Containers, they have added a feature that makes working with environment variables much easier (and made me happy as a result Smile).

Before this update you could take a couple of approaches to environment variables. One was to have separate Dockerfiles for different environments, as you can use the ENV instruction to set environment variables. I don’t like this approach because I generally prefer to have a single docker image with configuration applied externally.

The other approach is to use the fact that the VS Docker Tools generate PowerShell (and bash) scripts for performing the publishing. These aren’t just files that you can use as an example if you want to automate publishing; the tools generate and then use these scripts to perform the publish action. I think this is a great decision by the team! Since you get separate files created per publish profile, you can create separate publish profiles for each environment and modify the script. This was typically the approach I used, but it did leave some of the configuration tucked out of the way where it was easily forgotten.

In the recent update they’ve updated the experience so that you can specify additional Run options in the wizard, and these are passed into the script.

image

With this you can inject extra arguments into the “docker run” command. For example, to add environment variables Smile

With this update I now get the experience that I wanted to start with, i.e. the ability to have a single Dockerfile (and image), and to put the environmental differences into the publishing step. Enjoy!