Skip to content

Improve error reporting when loading the Docker configuration file #1263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

0xced
Copy link
Contributor

@0xced 0xced commented Sep 19, 2024

What does this PR do?

This pull request improves error handling while reading the Docker context configuration file. It throws a new DockerConfigurationException instead of silently failing.

Also, since GetCurrentEndpoint() can't return null anymore there's no need to try the default GetSocketPathFromHomeDesktopDir() and GetSocketPathFromHomeRunDir() socket paths.

Why is it important?

Silently returning null instead of throwing proper exceptions makes it harder to diagnose issues.

Related issues

How to test this PR

The DockerConfigTests were updated and a new ThrowsWhenDockerConfigEndpointNotFound test was added.

Copy link

netlify bot commented Sep 19, 2024

Deploy Preview for testcontainers-dotnet ready!

Name Link
🔨 Latest commit 69fff92
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-dotnet/deploys/68149d84bae16500084fd1d7
😎 Deploy Preview https://deploy-preview-1263--testcontainers-dotnet.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@0xced 0xced force-pushed the improve-docker-config-errors branch 7 times, most recently from f5a5f7a to 1e95bea Compare November 26, 2024 14:15
@0xced 0xced force-pushed the improve-docker-config-errors branch from 1e95bea to 11cf6cc Compare November 26, 2024 16:29
@0xced 0xced force-pushed the improve-docker-config-errors branch from 11cf6cc to a527375 Compare December 20, 2024 13:39
@0xced 0xced force-pushed the improve-docker-config-errors branch from a527375 to beaa485 Compare February 17, 2025 08:50
@0xced 0xced force-pushed the improve-docker-config-errors branch from beaa485 to 43b87e5 Compare February 24, 2025 19:11
@0xced 0xced force-pushed the improve-docker-config-errors branch from 43b87e5 to 6641034 Compare March 27, 2025 14:47
Comment on lines -107 to 123
using (var metaFileStream = File.OpenRead(metaFilePath))
catch (Exception notFoundException) when (notFoundException is DirectoryNotFoundException or FileNotFoundException)
{
var meta = JsonSerializer.Deserialize(metaFileStream, SourceGenerationContext.Default.DockerContextMeta);
var host = meta?.Name == dockerContext ? meta.Endpoints?.Docker?.Host : null;
return string.IsNullOrEmpty(host) ? null : new Uri(host.Replace("npipe:////./", "npipe://./"));
throw new DockerConfigurationException($"The Docker context '{dockerContext}' does not exist", notFoundException);
}
catch (Exception exception) when (exception is not DockerConfigurationException)
{
throw new DockerConfigurationException($"The Docker context '{dockerContext}' failed to load from {metaFilePath}", exception);
}
Copy link
Collaborator

@HofmeisterAn HofmeisterAn Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to explain my concerns. Maybe this is hypothetical, but let's assume:

  • The ~/.docker/config.json file exists.
  • The file contains a currentContext node.
  • The meta directory or file does not exist, or the currentContext cannot be found.
  • The user runs a remote container runtime, e.g. configured with WithDockerEndpoint(Uri).

With this implementation, an exception will be thrown when creating the list of Docker endpoints to check for availability.

System.TypeInitializationException
The type initializer for 'DotNet.Testcontainers.Configurations.TestcontainersSettings' threw an exception.

DotNet.Testcontainers.Builders.DockerConfigurationException
The Docker context '...' does not exist

System.IO.DirectoryNotFoundException
Could not find a part of the path '...'.

This prevents users from overriding the builder configuration using the API mentioned above. That said, I don't think WithDockerEndpoint(Uri) is the real issue here. If a user falls back to this API by default, something else is probably wrong.

My main concern is that this exception could also happen in other configurations. I believe this was one of the reasons we didn't throw exceptions in the initial implementation of Docker context support (IIRC, we removed it on purpose). Until now, we've been failing silently allowing another provider to find an available Docker endpoint.

The DockerConfigTests were updated and a new ThrowsWhenDockerConfigEndpointNotFound test was added.

I definitely agree with you on this. It bothers me too. I don't have a strong opinion on this PR. I was considering adding more logging to show which provider is processed and chosen (and which ones aren't). But if you don't see any issues with throwing an exception, we can go ahead and merge it. LMKWYT.

Copy link
Contributor Author

@0xced 0xced Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it's a bit unfortunate to throw if a valid endpoint is configured through WithDockerEndpoint(Uri). On the other hand, not throwing in this case makes it really hard to diagnose a broken Docker Desktop configuration.

Also, this case should really be exceptional and I think it warrants an exception that must be addressed by actually fixing the Docker Desktop configuration.

There's also this important change in the DockerDesktopEndpointAuthenticationProvider:

diff --git b/src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs a/src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs
index f2363f89..e1352585 100644
--- b/src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs
+++ a/src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs
@@ -15,7 +15,7 @@ namespace DotNet.Testcontainers.Builders
     /// Initializes a new instance of the <see cref="DockerDesktopEndpointAuthenticationProvider" /> class.
     /// </summary>
     public DockerDesktopEndpointAuthenticationProvider()
-      : base(DockerConfig.Instance.GetCurrentEndpoint()?.AbsolutePath, GetSocketPathFromHomeDesktopDir(), GetSocketPathFromHomeRunDir())
+      : base(DockerConfig.Instance.GetCurrentEndpoint())
     {
     }

It means that Testcontainers won't blindly try to connect to potentially valid sockets (from home desktop or home run directories). I've experienced issues doing so in the past where the socket was existing and but connecting to the socket would just stall because the engine was not running. It was very hard to diagnose!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants