Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the use of '--network' container option #3622

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
33 changes: 23 additions & 10 deletions src/Runner.Worker/ContainerOperationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,21 @@ public async Task StartContainersAsync(IExecutionContext executionContext, objec
}
executionContext.Output("##[endgroup]");

// 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);
executionContext.JobContext.Container["network"] = new StringContextData(containerNetwork);
executionContext.Output("##[endgroup]");

foreach (var container in containers)
{
string containerNetwork = null;
// Do not create a network if user specified --network within container options
if (!container.ContainerCreateOptions.Contains("--network"))
{
// Create local docker network for this job to avoid port conflict when multiple runners run on same machine.
executionContext.Output("##[group]Create local container network");
containerNetwork = $"github_network_{Guid.NewGuid():N}";

await CreateContainerNetworkAsync(executionContext, containerNetwork);
executionContext.JobContext.Container["network"] = new StringContextData(containerNetwork);
executionContext.Output("##[endgroup]");
}

container.ContainerNetwork = containerNetwork;
await StartContainerAsync(executionContext, container);
}
Expand Down Expand Up @@ -159,8 +164,16 @@ public async Task StopContainersAsync(IExecutionContext executionContext, object
{
await StopContainerAsync(executionContext, container);
}
// Remove the container network
await RemoveContainerNetworkAsync(executionContext, containers.First().ContainerNetwork);
var removedNetworks = new HashSet<string>();
foreach (var container in containers)
{
if (!string.IsNullOrEmpty(container.ContainerNetwork) && !removedNetworks.Contains(container.ContainerNetwork))
{
// Remove the container network
await RemoveContainerNetworkAsync(executionContext, container.ContainerNetwork);
removedNetworks.Add(container.ContainerNetwork);
}
}
}

private async Task StartContainerAsync(IExecutionContext executionContext, ContainerInfo container)
Expand Down