Docker on Azure - configuration and environment variables

Goal

Let’s assume wa want to load different connection string for application deployed to Azure App Service but we don’t want to change original configuration file appsettings.json.

In order to do that we should simply load additional configuration from file appsetting.{Environment}.json, in our case appsettings.AzureDev.json.

No alt text provided for this image

For that we just need to set environment variable DOTNET_ENVIROMENT to AzureDev.

App Service Configuration

Azure App Service has very convinient way to configure your application on the fly, without accessing file system. For example we want to set some environment variable or load different connection string. Simple as that…

No alt text provided for this image

But not really…

Apparently docker container is running inside another contenerized environment (responsible for Azure App Service) and DOTNET_ENVIRONMENT variable is not visible inside our container.

We can check that by connecting to SSH (see my previous article: Docker on Azure – SSH connection).

No alt text provided for this image

In theory we should fixed that by adding additional parameter to startup command.

No alt text provided for this image

But unfortunetaly after restarting App Service app is down, beacuse this is being added to the end of command and renders entire startup command invalid.

No alt text provided for this image

The real solution

In order to pass environment variables from Azure App Service configuration we need to add special command to docker image startup command in startup.sh file (see my previous article: Docker on Azure – SSH connection).

echo "passing env from outer container to inner container"
eval $(printenv | sed -n "s/^\([^=]\+\)=\(.*\)$/export \1=\2/p" | sed 's/"/\\\"/g' | sed '/=/s//="/' | sed 's/$/"/' >> /etc/profile)

Now after new app deployment all required variables are visible in docker container.

No alt text provided for this image

Now file appsettings.AzureDev.json is loaded.

Disclaimer – since now you can also modify connection string directly from Azure App Service configuration page.