Skip to content

Commit 557bbf7

Browse files
authored
fix dotnet#7916 by using DOTNET_RUNNING_IN_CONTAINERS env var to detect container (dotnet#9903)
1 parent e6db096 commit 557bbf7

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/DataProtection/DataProtection/src/Internal/DockerUtils.cs renamed to src/DataProtection/DataProtection/src/Internal/ContainerUtils.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
namespace Microsoft.AspNetCore.DataProtection.Internal
1111
{
12-
internal static class DockerUtils
12+
internal static class ContainerUtils
1313
{
14-
private static Lazy<bool> _isDocker = new Lazy<bool>(IsProcessRunningInDocker);
14+
private static Lazy<bool> _isContainer = new Lazy<bool>(IsProcessRunningInContainer);
1515

16-
public static bool IsDocker => _isDocker.Value;
16+
public static bool IsContainer => _isContainer.Value;
1717

1818
public static bool IsVolumeMountedFolder(DirectoryInfo directory)
1919
{
20-
if (!IsDocker)
20+
if (!IsContainer)
2121
{
2222
return false;
2323
}
@@ -77,14 +77,21 @@ internal static bool IsDirectoryMounted(DirectoryInfo directory, IEnumerable<str
7777
return false;
7878
}
7979

80-
private static bool IsProcessRunningInDocker()
80+
private static bool IsProcessRunningInContainer()
8181
{
82+
// Official .NET Core images (Windows and Linux) set this. So trust it if it's there.
83+
if(string.Equals(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINERS"), "true", StringComparison.OrdinalIgnoreCase))
84+
{
85+
return true;
86+
}
87+
8288
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
8389
{
8490
// we currently don't have a good way to detect if running in a Windows container
8591
return false;
8692
}
8793

94+
// Try to detect docker using the cgroups process 1 is in.
8895
const string procFile = "/proc/1/cgroup";
8996
if (!File.Exists(procFile))
9097
{

src/DataProtection/DataProtection/src/Repositories/FileSystemXmlRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public FileSystemXmlRepository(DirectoryInfo directory, ILoggerFactory loggerFac
3131

3232
try
3333
{
34-
if (DockerUtils.IsDocker && !DockerUtils.IsVolumeMountedFolder(Directory))
34+
if (ContainerUtils.IsContainer && !ContainerUtils.IsVolumeMountedFolder(Directory))
3535
{
3636
// warn users that keys may be lost when running in docker without a volume mounted folder
3737
_logger.UsingEphemeralFileSystemLocationInContainer(Directory.FullName);

src/DataProtection/DataProtection/test/DockerUtilsTests.cs renamed to src/DataProtection/DataProtection/test/ContainerUtilsTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.IO;
@@ -8,7 +8,7 @@
88

99
namespace Microsoft.AspNetCore.DataProtection.Test
1010
{
11-
public class DockerUtilsTests
11+
public class ContainerUtilsTests
1212
{
1313
// example of content from /proc/self/mounts
1414
private static readonly string[] fstab = new []
@@ -37,7 +37,7 @@ public class DockerUtilsTests
3737
[InlineData("../dir")]
3838
public void DeterminesFolderIsNotMounted(string directory)
3939
{
40-
Assert.False(DockerUtils.IsDirectoryMounted(new DirectoryInfo(directory), fstab));
40+
Assert.False(ContainerUtils.IsDirectoryMounted(new DirectoryInfo(directory), fstab));
4141
}
4242

4343
[ConditionalTheory]
@@ -50,7 +50,7 @@ public void DeterminesFolderIsNotMounted(string directory)
5050
[InlineData("/app/subdir/two/")]
5151
public void DeterminesFolderIsMounted(string directory)
5252
{
53-
Assert.True(DockerUtils.IsDirectoryMounted(new DirectoryInfo(directory), fstab));
53+
Assert.True(ContainerUtils.IsDirectoryMounted(new DirectoryInfo(directory), fstab));
5454
}
5555
}
5656
}

0 commit comments

Comments
 (0)