diff --git a/src/DataProtection/DataProtection/src/Internal/ContainerUtils.cs b/src/DataProtection/DataProtection/src/Internal/ContainerUtils.cs index 01547be110..7e282d4829 100644 --- a/src/DataProtection/DataProtection/src/Internal/ContainerUtils.cs +++ b/src/DataProtection/DataProtection/src/Internal/ContainerUtils.cs @@ -12,6 +12,8 @@ namespace Microsoft.AspNetCore.DataProtection.Internal internal static class ContainerUtils { private static Lazy _isContainer = new Lazy(IsProcessRunningInContainer); + private const string RunningInContainerVariableName = "DOTNET_RUNNING_IN_CONTAINER"; + private const string DeprecatedRunningInContainerVariableName = "DOTNET_RUNNING_IN_CONTAINERS"; public static bool IsContainer => _isContainer.Value; @@ -80,7 +82,8 @@ namespace Microsoft.AspNetCore.DataProtection.Internal private static bool IsProcessRunningInContainer() { // Official .NET Core images (Windows and Linux) set this. So trust it if it's there. - if(string.Equals(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINERS"), "true", StringComparison.OrdinalIgnoreCase)) + // We check both DOTNET_RUNNING_IN_CONTAINER (the current name) and DOTNET_RUNNING_IN_CONTAINERS (a deprecated name used in some images). + if (GetBooleanEnvVar(RunningInContainerVariableName) || GetBooleanEnvVar(DeprecatedRunningInContainerVariableName)) { return true; } @@ -102,5 +105,12 @@ namespace Microsoft.AspNetCore.DataProtection.Internal // typically the last line in the file is "1:name=openrc:/docker" return lines.Reverse().Any(l => l.EndsWith("name=openrc:/docker", StringComparison.Ordinal)); } + + private static bool GetBooleanEnvVar(string envVarName) + { + var value = Environment.GetEnvironmentVariable(envVarName); + return string.Equals(value, "1", StringComparison.Ordinal) || + string.Equals(value, "true", StringComparison.OrdinalIgnoreCase); + } } }