Skip to content

Commit aa60b63

Browse files
authored
fix: try different connections
1 parent e9bfb61 commit aa60b63

File tree

1 file changed

+94
-8
lines changed

1 file changed

+94
-8
lines changed

src/Sergen.Core/Services/Containers/Docker/DockerInterface.cs

Lines changed: 94 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,100 @@ public DockerInterface (ILogger<DockerInterface> logger, IOptions<SteamLoginOpti
3838
_fileStore = fileStore;
3939
var os = Environment.OSVersion;
4040

41-
if (os.Platform == PlatformID.Unix) {
42-
_client = new DockerClientConfiguration (
43-
new Uri ("unix:///var/run/docker.sock"))
44-
.CreateClient ();
45-
} else if (os.Platform == PlatformID.Win32NT) {
46-
_client = new DockerClientConfiguration (
47-
new Uri ("npipe://./pipe/docker_engine"))
48-
.CreateClient ();
41+
try
42+
{
43+
if (os.Platform == PlatformID.Unix)
44+
{
45+
// Try rootless Podman socket first
46+
var podmanSocket = Environment.GetEnvironmentVariable("XDG_RUNTIME_DIR");
47+
if (!string.IsNullOrEmpty(podmanSocket))
48+
{
49+
try
50+
{
51+
_client = new DockerClientConfiguration(
52+
new Uri($"unix://{podmanSocket}/podman/podman.sock"))
53+
.CreateClient();
54+
_logger.LogInformation("Connected to Podman rootless socket.");
55+
}
56+
catch (Exception ex)
57+
{
58+
_logger.LogError(ex, $"Error connecting to Podman rootless socket. {ex.Message}");
59+
}
60+
}
61+
62+
// Try Podman rootful socket if rootless failed or was not applicable
63+
if (_client == null)
64+
{
65+
try
66+
{
67+
_client = new DockerClientConfiguration(
68+
new Uri("unix:///run/podman/podman.sock"))
69+
.CreateClient();
70+
_logger.LogInformation("Connected to Podman rootful socket.");
71+
}
72+
catch (Exception ex)
73+
{
74+
_logger.LogError(ex, $"Error connecting to Podman rootful socket. {ex.Message}");
75+
}
76+
}
77+
78+
// Try Docker socket as a fallback if Podman failed
79+
if (_client == null)
80+
{
81+
try
82+
{
83+
_client = new DockerClientConfiguration(
84+
new Uri("unix:///var/run/docker.sock"))
85+
.CreateClient();
86+
_logger.LogInformation("Connected to Docker socket.");
87+
}
88+
catch (Exception ex)
89+
{
90+
_logger.LogError(ex, $"Error connecting to Docker socket. {ex.Message}");
91+
}
92+
}
93+
}
94+
else if (os.Platform == PlatformID.Win32NT)
95+
{
96+
// Try named pipe for Docker
97+
try
98+
{
99+
_client = new DockerClientConfiguration(
100+
new Uri("npipe://./pipe/docker_engine"))
101+
.CreateClient();
102+
_logger.LogInformation("Connected to Docker named pipe.");
103+
}
104+
catch (Exception ex)
105+
{
106+
_logger.LogError(ex, $"Error connecting to Docker named pipe. {ex.Message}");
107+
}
108+
109+
// Try TCP endpoint for Podman (if configured to expose it)
110+
if (_client == null)
111+
{
112+
try
113+
{
114+
_client = new DockerClientConfiguration(
115+
new Uri("tcp://localhost:2375")) // Adjust the port as needed
116+
.CreateClient();
117+
_logger.LogInformation("Connected to Podman TCP socket.");
118+
}
119+
catch (Exception ex)
120+
{
121+
_logger.LogError(ex, $"Error connecting to Podman TCP socket. {ex.Message}");
122+
}
123+
}
124+
}
125+
126+
// Log if no connection could be made
127+
if (_client == null)
128+
{
129+
_logger.LogError("Failed to connect to both Docker and Podman.");
130+
}
131+
}
132+
catch (Exception ex)
133+
{
134+
_logger.LogError(ex, $"Error setting up docker client: {ex.Message}");
49135
}
50136
}
51137

0 commit comments

Comments
 (0)