Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 60 additions & 18 deletions src/Aspire.Hosting/Publishing/DockerContainerRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,43 +131,85 @@ public async Task BuildImageAsync(string contextPath, string dockerfilePath, str
}
}

public Task<bool> CheckIfRunningAsync(CancellationToken cancellationToken)
public async Task<bool> CheckIfRunningAsync(CancellationToken cancellationToken)
{
var spec = new ProcessSpec("docker")
// First check if Docker daemon is running using the same check that DCP uses
if (!await CheckDockerDaemonAsync(cancellationToken).ConfigureAwait(false))
{
Arguments = "buildx version",
return false;
}

// Then check if Docker buildx is available
return await CheckDockerBuildxAsync(cancellationToken).ConfigureAwait(false);
}

private async Task<bool> CheckDockerDaemonAsync(CancellationToken cancellationToken)
{
var dockerRunningSpec = new ProcessSpec("docker")
{
Arguments = "container ls -n 1",
OnOutputData = output =>
{
logger.LogInformation("docker buildx version (stdout): {Output}", output);
logger.LogInformation("docker container ls (stdout): {Output}", output);
},
OnErrorData = error =>
{
logger.LogInformation("docker buildx version (stderr): {Error}", error);
logger.LogInformation("docker container ls (stderr): {Error}", error);
Copy link

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider using LogWarning or LogError for stderr output to better reflect that this data represents an error stream rather than informational messages.

Suggested change
logger.LogInformation("docker container ls (stderr): {Error}", error);
logger.LogWarning("docker container ls (stderr): {Error}", error);

Copilot uses AI. Check for mistakes.
},
Comment on lines 151 to 158
Copy link

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

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

[nitpick] The OnOutputData and OnErrorData logging closures are duplicated in both health-check methods; consider extracting a shared helper to reduce duplication and improve maintainability.

Suggested change
OnOutputData = output =>
{
logger.LogInformation("docker buildx version (stdout): {Output}", output);
logger.LogInformation("docker container ls (stdout): {Output}", output);
},
OnErrorData = error =>
{
logger.LogInformation("docker buildx version (stderr): {Error}", error);
logger.LogInformation("docker container ls (stderr): {Error}", error);
},
OnOutputData = output => LogProcessOutput("stdout", output),
OnErrorData = error => LogProcessOutput("stderr", error),

Copilot uses AI. Check for mistakes.
ThrowOnNonZeroReturnCode = false,
InheritEnv = true
};

logger.LogInformation("Running Docker CLI with arguments: {ArgumentList}", spec.Arguments);
var (pendingProcessResult, processDisposable) = ProcessUtil.Run(spec);
logger.LogInformation("Checking if Docker daemon is running with arguments: {ArgumentList}", dockerRunningSpec.Arguments);
var (pendingDockerResult, dockerDisposable) = ProcessUtil.Run(dockerRunningSpec);

await using (dockerDisposable)
{
var dockerResult = await pendingDockerResult.WaitAsync(cancellationToken).ConfigureAwait(false);

return CheckDockerBuildxAsync(pendingProcessResult, processDisposable, cancellationToken);
if (dockerResult.ExitCode != 0)
{
logger.LogError("Docker daemon is not running. Exit code: {ExitCode}.", dockerResult.ExitCode);
return false;
}

async Task<bool> CheckDockerBuildxAsync(Task<ProcessResult> pendingResult, IAsyncDisposable processDisposable, CancellationToken ct)
logger.LogInformation("Docker daemon is running.");
return true;
}
}

private async Task<bool> CheckDockerBuildxAsync(CancellationToken cancellationToken)
{
var buildxSpec = new ProcessSpec("docker")
{
await using (processDisposable)
Arguments = "buildx version",
OnOutputData = output =>
{
logger.LogInformation("docker buildx version (stdout): {Output}", output);
},
OnErrorData = error =>
{
var processResult = await pendingResult.WaitAsync(ct).ConfigureAwait(false);
logger.LogInformation("docker buildx version (stderr): {Error}", error);
},
ThrowOnNonZeroReturnCode = false,
InheritEnv = true
};

if (processResult.ExitCode != 0)
{
logger.LogError("Docker buildx version failed with exit code {ExitCode}.", processResult.ExitCode);
return false;
}
logger.LogInformation("Checking Docker buildx with arguments: {ArgumentList}", buildxSpec.Arguments);
var (pendingBuildxResult, buildxDisposable) = ProcessUtil.Run(buildxSpec);

logger.LogInformation("Docker buildx is available and running.");
return true;
await using (buildxDisposable)
{
var buildxResult = await pendingBuildxResult.WaitAsync(cancellationToken).ConfigureAwait(false);

if (buildxResult.ExitCode != 0)
{
logger.LogError("Docker buildx version failed with exit code {ExitCode}.", buildxResult.ExitCode);
return false;
}

logger.LogInformation("Docker buildx is available and running.");
return true;
}
}

Expand Down
Loading