Skip to content

Commit

Permalink
Fix docker inspect failure
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherHX committed Mar 1, 2022
1 parent 5cae60f commit 30040ec
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/Runner.Worker/ContainerOperationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,15 @@ public async Task StartContainersAsync(IExecutionContext executionContext, objec
}
executionContext.Output("##[endgroup]");

var platform = (await HostContext.GetService<IDockerCommandManager>().DockerInspect(executionContext, containers[0].ContainerImage, "--format=\"{{.Os}}/{{.Architecture}}\"")).FirstOrDefault();
if(string.IsNullOrEmpty(platform) || !platform.Contains('/')) {
throw new Exception("Failed to determine container platform");
}
var plat = platform.Split('/', 2);
var os = plat[0];
var arch = plat[1];

// Create local docker network for this job to avoid port conflict when multiple runners run on same machine.
// All containers within a job join the same network
executionContext.Output("##[group]Create local container network");
var containerNetwork = $"github_network_{Guid.NewGuid().ToString("N")}";
await CreateContainerNetworkAsync(executionContext, containerNetwork, os == "windows" ? "nat" : null);
executionContext.JobContext.Container["network"] = new StringContextData(containerNetwork);
executionContext.Output("##[endgroup]");
string containerNetwork = null;

foreach (var container in containers)
{
container.ContainerNetwork = containerNetwork;
container.Os = os;
container.Arch = arch;
await StartContainerAsync(executionContext, container);
if(containerNetwork == null) {
containerNetwork = container.ContainerNetwork;
}
}

executionContext.Output("##[group]Waiting for all services to be ready");
Expand Down Expand Up @@ -162,6 +149,17 @@ public async Task StopContainersAsync(IExecutionContext executionContext, object
}
}

private async Task CreateContainerNetwork(IExecutionContext executionContext, ContainerInfo container) {
// Create local docker network for this job to avoid port conflict when multiple runners run on same machine.
// All containers within a job join the same network
executionContext.Output("##[group]Create local container network");
var containerNetwork = $"github_network_{Guid.NewGuid().ToString("N")}";
await CreateContainerNetworkAsync(executionContext, containerNetwork, container.Os == "windows" ? "nat" : null);
executionContext.JobContext.Container["network"] = new StringContextData(containerNetwork);
executionContext.Output("##[endgroup]");
container.ContainerNetwork = containerNetwork;
}

private async Task StartContainerAsync(IExecutionContext executionContext, ContainerInfo container)
{
Trace.Entering();
Expand Down Expand Up @@ -225,6 +223,18 @@ private async Task StartContainerAsync(IExecutionContext executionContext, Conta
throw new InvalidOperationException($"Docker pull failed with exit code {pullExitCode}");
}

var platform = (await HostContext.GetService<IDockerCommandManager>().DockerInspect(executionContext, container.ContainerImage, "--format=\"{{.Os}}/{{.Architecture}}\"")).FirstOrDefault();
if(string.IsNullOrEmpty(platform) || !platform.Contains('/')) {
throw new Exception("Failed to determine container platform");
}
var plat = platform.Split('/', 2);
container.Os = plat[0];
container.Arch = plat[1];

if(container.ContainerNetwork == null) {
await CreateContainerNetwork(executionContext, container);
}

if (container.IsJobContainer)
{
// Configure job container - Mount workspace and tools, set up environment, and start long running process
Expand Down

0 comments on commit 30040ec

Please sign in to comment.