Skip to content

Commit bf2e7b8

Browse files
committed
Add support for additional Docker arguments
Introduced the `AdditionalArguments` property in `DockerRunOptions` to allow specifying extra Docker command arguments. Added the `GetDockerModeAsync` method to `IDockerService` and implemented it in `DockerService` to dynamically determine the Docker operating system type (Linux, Windows, or Unknown) using the `docker info` command. Updated `RunContainerAsync` in `DockerService` to include `AdditionalArguments` in Docker commands. Refactored `InitCommandOptionsHandler` to use `GetDockerModeAsync` for platform detection and set `AdditionalArguments` to `" --start"` when creating Docker containers. Removed an extraneous closing brace in `InitCommand`. These changes improve platform-awareness and enhance flexibility in Docker command execution. #135
1 parent 3ae51b3 commit bf2e7b8

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

src/FlowCtl.Core/Models/Docker/DockerRunOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public class DockerRunOptions
1010
public string HostDataPath { get; set; } = string.Empty;
1111
public string ContainerDataPath { get; set; } = string.Empty;
1212
public bool Detached { get; set; } = true;
13+
public string? AdditionalArguments { get; set; } = string.Empty;
1314
}

src/FlowCtl.Core/Services/Docker/IDockerService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace FlowCtl.Core.Services.Docker;
44

55
public interface IDockerService
66
{
7+
Task<string> GetDockerModeAsync(CancellationToken cancellationToken = default);
78
Task<bool> IsDockerAvailableAsync(CancellationToken cancellationToken = default);
89
Task<DockerCommandResult> PullImageAsync(string imageName, string tag, CancellationToken cancellationToken = default);
910
Task<DockerCommandResult> RunContainerAsync(DockerRunOptions options, CancellationToken cancellationToken = default);

src/FlowCtl.Infrastructure/Services/Docker/DockerService.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ public DockerService(IFlowCtlLogger flowCtlLogger)
1515
_flowCtlLogger = flowCtlLogger ?? throw new ArgumentNullException(nameof(flowCtlLogger));
1616
}
1717

18+
public async Task<string> GetDockerModeAsync(CancellationToken cancellationToken = default)
19+
{
20+
var result = await RunDockerAsync(new[] { "info", "--format", "{{.OSType}}" }, streamOutput: false, cancellationToken);
21+
if (!result.Success || string.IsNullOrWhiteSpace(result.Output))
22+
{
23+
return "Unknown";
24+
}
25+
26+
// Docker OSType usually returns "linux" or "windows"
27+
var osType = result.Output.Trim().ToLowerInvariant();
28+
return osType switch
29+
{
30+
"linux" => "Linux",
31+
"windows" => "Windows",
32+
_ => "Unknown"
33+
};
34+
}
35+
1836
public Task<bool> IsDockerAvailableAsync(CancellationToken cancellationToken = default)
1937
{
2038
return ContainerQueryAsync(new[] { "info" }, cancellationToken);
@@ -65,6 +83,11 @@ public Task<DockerCommandResult> RunContainerAsync(DockerRunOptions options, Can
6583

6684
arguments.Add($"{options.ImageName}:{options.Tag}");
6785

86+
if (!string.IsNullOrWhiteSpace(options.AdditionalArguments))
87+
{
88+
arguments.AddRange(options.AdditionalArguments.Split(' ', StringSplitOptions.RemoveEmptyEntries));
89+
}
90+
6891
return RunDockerAsync(arguments, streamOutput: true, cancellationToken);
6992
}
7093

src/FlowCtl/Commands/Init/InitCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ public InitCommand() : base("init", Resources.Commands_Init_Description)
3939
AddOption(mountOption);
4040
AddOption(containerPathOption);
4141
}
42-
}
42+
}

src/FlowCtl/Commands/Init/InitCommandOptionsHandler.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private async Task InitializeDockerAsync(InitCommandOptions options, Cancellatio
135135
return;
136136
}
137137

138-
var platformSuffix = GetPlatformSuffix();
138+
var platformSuffix = await GetPlatformSuffix(cancellationToken);
139139
if (string.IsNullOrWhiteSpace(platformSuffix))
140140
{
141141
_flowCtlLogger.WriteError(Resources.Commands_Init_UnsupportedPlatform);
@@ -170,7 +170,8 @@ private async Task InitializeDockerAsync(InitCommandOptions options, Cancellatio
170170
ContainerPort = EngineContainerPort,
171171
HostDataPath = hostDataPath,
172172
ContainerDataPath = containerDataPath,
173-
Detached = true
173+
Detached = true,
174+
AdditionalArguments = " --start"
174175
};
175176

176177
_flowCtlLogger.Write(string.Format(Resources.Commands_Init_CreatingDockerContainer, containerName, port));
@@ -353,14 +354,12 @@ private async Task<string> ResolveFlowSynxImageVersion(string? requestedVersion,
353354
return NormalizeDockerVersion(version);
354355
}
355356

356-
private string? GetPlatformSuffix()
357+
private async Task<string?> GetPlatformSuffix(CancellationToken cancellationToken)
357358
{
358-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
359+
var mode = await _dockerService.GetDockerModeAsync(cancellationToken);
360+
if (mode == "Windows")
359361
return "windows-ltsc2022-amd64";
360362

361-
if (RuntimeInformation.ProcessArchitecture != Architecture.X64)
362-
return null;
363-
364363
return "linux-amd64";
365364
}
366365

0 commit comments

Comments
 (0)