How to use Octopus Deploy Configuration Transforms to transform configuration files in a Docker Container?

Hi,

I am getting ready to use Docker for an ASP.NET application and I’m trying to figure out how I would do configuration transforms when the configuration file is inside the container.

Currently, I use TeamCity to build and the deploy goes through Octopus Deploy. In summary, the Docker Images would be built in TeamCity and push to the Container Registry after tagging it.

How am I going to transform files using Octopus Env. Variables if the images are created and cannot be modified? Is there a way to accomplish it?

How do other people accomplish transforming configurations for containers? Does anybody else have experience with transforming from octopus to files inside a container or some other method of doing it?

Hello @vinicius.deschamps,

Thanks for reaching out, and welcome to the community!

Will these containers be deployed and run on a server, or will they be deployed to a Kubernetes cluster?

Best regards,
Mark Butler

Hi @mark.butler thanks for your prompt response and the warm welcome.

They are going to be deployed to a Kubernetes cluster (Azure Kubernetes Services)

Hello @vinicius.deschamps,

As I was typing my response, I realized I was assuming you wrote your application with ASP.NET core, and you’d be transforming appsettings.json files, but I wanted to confirm that this was the case?

Best regards,
Mark Butler

Hi @mark.butler,

The application I am using is Sitecore 10 so it uses ASP.NET Core with Containers.

I am going to be transforming .Config files, like Web.config and ConnectionStrings.config. I don’t think I am going to modify JSON but thanks for checking.

As a side note, Sitecore uses docker-compose and Dockerfile to build their images. And it also uses a Solution image to perform msbuild.

Thanks,
Vinicius

Hello @vinicius.deschamps,

Thank you for providing me with that information.

Application containerization is focused around the concept of immutable infrastructure. Specifically, this involves the application code being baked into the container image, but environment-specific configuration settings (such as appsettings) are injected into the container at run time. This makes the container image re-usable across your environments, rather than having to build an image-per-environment.

For .NET applications to adhere to this concept, they typically need to be configured to reference environment variables over performing traditional config transforms and then using those resulting files for the source of configuration settings.

There are then a few ways to map Octopus project variables to these environment variables for containers to use at runtime.

The simplest way, in my opinion, if you don’t have a lot of variables is to map each one using the Environment Variables section of the Add Container section of the Deploy Kubernetes Container step template.

In my example, I have three project variables (DefaultConnection, EnvironmentName, AppVersion) and have mapped each one to an environment variable for the container to use.

In that section of the step, you can also map environment variables from a Kubernetes ConfigMap and a Secret resource. You can either create these resources within the same step to reference or have separate steps that run before creating them.

Using these other Kubernetes resources can be beneficial when working with many project variables that need to be mapped to environment variables. You can script looping through each variable to create the resources instead of manually referencing each one or store the entire configuration to be replaced.

We have a few resources explaining in further detail how you can accomplish this:

I hope that this helps. Please feel free to let me know if anything needs further clarification or you have any other questions.

Best regards,
Mark Butler

Hi @mark.butler,

Thanks for your response but I do have tons of questions actually :grinning:

So, I have some understanding about exposing Octopus variable to a Kubernetes container using Secrets but still unclear how my pod application will know where to use that Secret.

Let’s assume I have successfully exposed Octopus variable to a Kubernetes container using Secrets, and I have a pod running my application.

My application needs to replace the Octopus variable in a specific config file, let’s say Custom-File.config that contains the following parameter

targetHostName="#{IntranetHostName}"

How this is going to be applied by the Secrets at the runtime and replaced by the correct value?

Thanks,
Vinicius

Hello @vinicius.deschamps,

Your application would need to be configured to use the environment variables instead of the Octopus variables.

Your Kubernetes secret resource would be created using the Octopus variables, and then you would reference that secret to map it to environment variables.

In this screenshot, I’m creating a Kubernetes secret called MySecret. It contains a key/value pair for the db connection string with the value mapped to my Octopus project variable.

When creating the container for my deployment, I can then reference that secret to add environment variables for each key/value pair in the secret for the container.

I’m not sure if it’s possible with your .config files to reference the environment variables, but in ASP.NET Core the default configuration builder in the start-up of the application is set up to load configuration from environment variables for your appsettings.

I hope this helps.

Best regards,
Mark Butler

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.