Override appsettings.json with environment variables in Docker, Kubernetes and Azure
Your container starts, but it connects to the wrong service. The environment variable exists. The JSON file looks right. The missing detail is often the separator: .NET configuration uses : for hierarchy, while portable environment variable names use __.
Payments__BaseUrl maps to Payments:BaseUrl. With the default ASP.NET Core configuration providers, that value overrides the matching entry in your JSON settings.
TL;DR
- Use double underscores for nested configuration keys.
- Default providers load environment variables after JSON files.
- Arrays use numeric key segments and do not behave like replaceable JSON documents.
- Keep secrets out of images, committed manifests and diagnostic dumps.
Convert the key path, not the JSON syntax
Consider this settings file:
{
"Payments": {
"BaseUrl": "https://sandbox.example.invalid",
"TimeoutSeconds": 10
},
"AllowedHosts": "*",
"Features": ["invoices", "exports"]
}
The corresponding names are:
Payments__BaseUrl=https://payments.example.invalid
Payments__TimeoutSeconds=30
AllowedHosts=example.invalid
Features__0=invoices
Features__1=exports
Use the appsettings.json to environment variables converter to flatten a larger file. Use redacted configuration when checking a mapping; a conversion task rarely needs production credentials.
Values arrive as strings. The configuration binder converts them to your options types. For a boolean, use an explicit value such as true, rather than treating the presence of a variable as a feature switch.
Precedence belongs to the provider order
For app configuration created by WebApplication.CreateBuilder(args), the usual progression is base JSON, environment-specific JSON, development user secrets, environment variables and command-line arguments. Later providers win for the same key.
That means an environment variable can override appsettings.Production.json, while a command-line value can override the environment variable.
Custom startup code can change that order. If your override is ignored, inspect provider registration before changing deployment syntax. Avoid dumping the entire configuration: it may expose secrets.
Docker passes variables into the process
For an already built ASP.NET Core image, use PowerShell:
docker run --rm `
--env "Payments__BaseUrl=https://payments.example.invalid" `
--env "Payments__TimeoutSeconds=30" `
my-api:local
A Compose service uses the same keys:
services:
api:
image: my-api:local
environment:
Payments__BaseUrl: "https://payments.example.invalid"
Payments__TimeoutSeconds: "30"
Compose’s project .env file primarily supplies interpolation values to Compose. It does not automatically inject every entry into a container. Use environment or an explicit env_file configuration.
Do not bake environment-specific secrets into Dockerfile ENV instructions. Image distribution and runtime configuration have different access boundaries.
Kubernetes keeps the mapping explicit
This is the container portion of a Deployment’s Pod template:
containers:
- name: api
image: my-api:local
env:
- name: Payments__TimeoutSeconds
value: "30"
- name: Payments__BaseUrl
valueFrom:
configMapKeyRef:
name: payments-config
key: base-url
The referenced ConfigMap must exist in the same namespace. The key base-url inside that ConfigMap does not need to be named like a .NET key, because the env entry assigns the process-facing name.
Use the appropriate secret mechanism for credentials. A Kubernetes Secret’s base64 representation is not encryption by itself. Control access and storage protection separately.
Environment variables are fixed for a running process. Updating a ConfigMap does not update the environment of existing Pods; roll them out again when those values must change.
Azure App Service uses the same .NET key names
In App Service’s application settings, add Payments__BaseUrl and Payments__TimeoutSeconds. These settings become environment variables for the application. Saving changes triggers an application restart.
Use slot settings deliberately when a value must remain tied to a deployment slot. For credentials, prefer managed identity where supported or use a suitable secret-reference mechanism. Ordinary application settings are configuration, not a reason to copy secrets into source.
ASPNETCORE_ENVIRONMENT chooses an environment; it does not turn a variable such as Payments_BaseUrl into a nested key. The hierarchy separator still needs two underscores.
Arrays are merged by key
With the earlier JSON, setting only Features__0=payments changes index zero. It does not necessarily remove index one from an earlier provider. Configuration is a collection of keys, not a JSON replacement operation.
For lists whose size changes per deployment, choose a documented representation your application owns, or supply the complete configuration through a suitable provider. Do not assume one environment variable replaces an entire section.
Validate options at startup:
using System.ComponentModel.DataAnnotations;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOptions<PaymentsOptions>()
.Bind(builder.Configuration.GetSection("Payments"))
.ValidateDataAnnotations()
.ValidateOnStart();
var app = builder.Build();
app.MapGet("/health", () => Results.Ok());
app.Run();
public sealed class PaymentsOptions
{
[Required, Url]
public string BaseUrl { get; set; } = "";
[Range(1, 120)]
public int TimeoutSeconds { get; set; }
}
This uses the standard ASP.NET Core shared framework. It checks shape and range, not whether the remote endpoint is reachable or approved. Deployment should fail clearly when required configuration is missing, rather than quietly selecting a convenient default.
Sources and related material
- Microsoft: ASP.NET Core configuration and provider precedence
- Docker: environment variables in Compose
- Kubernetes: environment variables from ConfigMaps
- Microsoft: configure an App Service application
- Microsoft: options validation
Thank you for reading.
-Bruno

