From 1ac66cb7096de027920513bf062031112cee01e4 Mon Sep 17 00:00:00 2001 From: Marko Kosmerl Date: Thu, 12 Dec 2024 17:57:37 -0300 Subject: [PATCH] fix: handle --network option --- .../ContainerOperationProvider.cs | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Runner.Worker/ContainerOperationProvider.cs b/src/Runner.Worker/ContainerOperationProvider.cs index c5cccb77ef0..b73dfa7b860 100644 --- a/src/Runner.Worker/ContainerOperationProvider.cs +++ b/src/Runner.Worker/ContainerOperationProvider.cs @@ -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); } @@ -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(); + 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)