-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerClientConfiguration.cs
More file actions
38 lines (32 loc) · 1.4 KB
/
DockerClientConfiguration.cs
File metadata and controls
38 lines (32 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using Docker.DotNet;
public static class DockerClientConfigurationExtensions
{
public static IServiceCollection AddDockerClient(this IServiceCollection services, IConfiguration configuration)
{
var dockerEndpoint = configuration.GetValue<string>("Docker:EndpointUri");
if (string.IsNullOrEmpty(dockerEndpoint))
{
dockerEndpoint = OperatingSystem.IsWindows()
? "npipe://./pipe/docker_engine"
: "unix:///var/run/docker.sock";
}
Console.WriteLine($"Using Docker Endpoint: {dockerEndpoint}");
services.AddSingleton<IDockerClient>(sp =>
{
var logger = sp.GetRequiredService<ILogger<DockerClient>>();
try
{
var client = new DockerClientConfiguration(new Uri(dockerEndpoint))
.CreateClient();
logger.LogInformation("Successfully created DockerClient connected to {Endpoint}", dockerEndpoint);
return client;
}
catch (Exception ex)
{
logger.LogCritical(ex, "Failed to create DockerClient connected to {Endpoint}. Ensure Docker daemon is running and accessible.", dockerEndpoint);
throw new InvalidOperationException($"Failed to initialize Docker client at {dockerEndpoint}", ex);
}
});
return services;
}
}