Skip to content

Commit f611600

Browse files
authored
Harden Docker container runtime health check (#10402)
1 parent 37d5759 commit f611600

File tree

2 files changed

+64
-22
lines changed

2 files changed

+64
-22
lines changed

src/Aspire.Hosting/Publishing/DockerContainerRuntime.cs

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,43 +131,85 @@ public async Task BuildImageAsync(string contextPath, string dockerfilePath, str
131131
}
132132
}
133133

134-
public Task<bool> CheckIfRunningAsync(CancellationToken cancellationToken)
134+
public async Task<bool> CheckIfRunningAsync(CancellationToken cancellationToken)
135135
{
136-
var spec = new ProcessSpec("docker")
136+
// First check if Docker daemon is running using the same check that DCP uses
137+
if (!await CheckDockerDaemonAsync(cancellationToken).ConfigureAwait(false))
137138
{
138-
Arguments = "buildx version",
139+
return false;
140+
}
141+
142+
// Then check if Docker buildx is available
143+
return await CheckDockerBuildxAsync(cancellationToken).ConfigureAwait(false);
144+
}
145+
146+
private async Task<bool> CheckDockerDaemonAsync(CancellationToken cancellationToken)
147+
{
148+
var dockerRunningSpec = new ProcessSpec("docker")
149+
{
150+
Arguments = "container ls -n 1",
139151
OnOutputData = output =>
140152
{
141-
logger.LogInformation("docker buildx version (stdout): {Output}", output);
153+
logger.LogInformation("docker container ls (stdout): {Output}", output);
142154
},
143155
OnErrorData = error =>
144156
{
145-
logger.LogInformation("docker buildx version (stderr): {Error}", error);
157+
logger.LogInformation("docker container ls (stderr): {Error}", error);
146158
},
147159
ThrowOnNonZeroReturnCode = false,
148160
InheritEnv = true
149161
};
150162

151-
logger.LogInformation("Running Docker CLI with arguments: {ArgumentList}", spec.Arguments);
152-
var (pendingProcessResult, processDisposable) = ProcessUtil.Run(spec);
163+
logger.LogInformation("Checking if Docker daemon is running with arguments: {ArgumentList}", dockerRunningSpec.Arguments);
164+
var (pendingDockerResult, dockerDisposable) = ProcessUtil.Run(dockerRunningSpec);
165+
166+
await using (dockerDisposable)
167+
{
168+
var dockerResult = await pendingDockerResult.WaitAsync(cancellationToken).ConfigureAwait(false);
153169

154-
return CheckDockerBuildxAsync(pendingProcessResult, processDisposable, cancellationToken);
170+
if (dockerResult.ExitCode != 0)
171+
{
172+
logger.LogError("Docker daemon is not running. Exit code: {ExitCode}.", dockerResult.ExitCode);
173+
return false;
174+
}
155175

156-
async Task<bool> CheckDockerBuildxAsync(Task<ProcessResult> pendingResult, IAsyncDisposable processDisposable, CancellationToken ct)
176+
logger.LogInformation("Docker daemon is running.");
177+
return true;
178+
}
179+
}
180+
181+
private async Task<bool> CheckDockerBuildxAsync(CancellationToken cancellationToken)
182+
{
183+
var buildxSpec = new ProcessSpec("docker")
157184
{
158-
await using (processDisposable)
185+
Arguments = "buildx version",
186+
OnOutputData = output =>
187+
{
188+
logger.LogInformation("docker buildx version (stdout): {Output}", output);
189+
},
190+
OnErrorData = error =>
159191
{
160-
var processResult = await pendingResult.WaitAsync(ct).ConfigureAwait(false);
192+
logger.LogInformation("docker buildx version (stderr): {Error}", error);
193+
},
194+
ThrowOnNonZeroReturnCode = false,
195+
InheritEnv = true
196+
};
161197

162-
if (processResult.ExitCode != 0)
163-
{
164-
logger.LogError("Docker buildx version failed with exit code {ExitCode}.", processResult.ExitCode);
165-
return false;
166-
}
198+
logger.LogInformation("Checking Docker buildx with arguments: {ArgumentList}", buildxSpec.Arguments);
199+
var (pendingBuildxResult, buildxDisposable) = ProcessUtil.Run(buildxSpec);
167200

168-
logger.LogInformation("Docker buildx is available and running.");
169-
return true;
201+
await using (buildxDisposable)
202+
{
203+
var buildxResult = await pendingBuildxResult.WaitAsync(cancellationToken).ConfigureAwait(false);
204+
205+
if (buildxResult.ExitCode != 0)
206+
{
207+
logger.LogError("Docker buildx version failed with exit code {ExitCode}.", buildxResult.ExitCode);
208+
return false;
170209
}
210+
211+
logger.LogInformation("Docker buildx is available and running.");
212+
return true;
171213
}
172214
}
173215

src/Aspire.Hosting/Publishing/PodmanContainerRuntime.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ public Task<bool> CheckIfRunningAsync(CancellationToken cancellationToken)
9797
{
9898
var spec = new ProcessSpec("podman")
9999
{
100-
Arguments = "info",
100+
Arguments = "container ls -n 1",
101101
OnOutputData = output =>
102102
{
103-
logger.LogInformation("podman info (stdout): {Output}", output);
103+
logger.LogInformation("podman container ls (stdout): {Output}", output);
104104
},
105105
OnErrorData = error =>
106106
{
107-
logger.LogInformation("podman info (stderr): {Error}", error);
107+
logger.LogInformation("podman container ls (stderr): {Error}", error);
108108
},
109109
ThrowOnNonZeroReturnCode = false,
110110
InheritEnv = true
@@ -123,7 +123,7 @@ async Task<bool> CheckPodmanHealthAsync(Task<ProcessResult> pending, IAsyncDispo
123123

124124
if (processResult.ExitCode != 0)
125125
{
126-
logger.LogError("Podman info failed with exit code {ExitCode}.", processResult.ExitCode);
126+
logger.LogError("Podman container ls failed with exit code {ExitCode}.", processResult.ExitCode);
127127
return false;
128128
}
129129

0 commit comments

Comments
 (0)