Skip to content

Commit 090eeeb

Browse files
committed
feat: update CalculationInstanceDockerAdapter to expose port
1 parent 544e73f commit 090eeeb

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/Adapters/Driven/Analysis.Instance.Docker/CalculationInstanceDockerAdapter.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,30 @@ public async Task<string> Allocate(
2727

2828
await PullImageIfNeeded(client, image.ImageName, cancellationToken);
2929

30+
var hostPort = freePortProvider.GetFreeTcpPort();
31+
32+
var portBindings = new Dictionary<string, IList<PortBinding>>
33+
{
34+
{
35+
$"{calculationDockerConfig.Port}/tcp",
36+
new List<PortBinding> { new() { HostPort = hostPort.ToString() } }
37+
},
38+
};
39+
3040
var response = await client.Containers.CreateContainerAsync(
3141
new CreateContainerParameters
3242
{
3343
Name = $"scriptbee-calculation-{instanceId}",
3444
Image = image.ImageName,
35-
HostConfig = new HostConfig { NetworkMode = calculationDockerConfig.Network },
45+
HostConfig = new HostConfig
46+
{
47+
NetworkMode = calculationDockerConfig.Network,
48+
PortBindings = portBindings,
49+
},
50+
ExposedPorts = new Dictionary<string, EmptyStruct>
51+
{
52+
{ $"{calculationDockerConfig.Port}/tcp", new EmptyStruct() },
53+
},
3654
},
3755
cancellationToken
3856
);
@@ -50,7 +68,7 @@ await client.Containers.StartContainerAsync(
5068
client,
5169
response.ID,
5270
calculationDockerConfig.Network,
53-
freePortProvider.GetFreeTcpPort(),
71+
hostPort,
5472
cancellationToken
5573
);
5674
}

src/Adapters/Driven/Analysis.Instance.Docker/Config/CalculationDockerConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ public class CalculationDockerConfig
44
{
55
public required string DockerSocket { get; init; }
66

7+
public int Port { get; init; } = 8080;
8+
79
public string? Network { get; init; }
810
}

test/Adapters/Driven/Analysis.Instance.Docker.Tests/CalculationInstanceDockerAdapterTest.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class CalculationInstanceDockerAdapterTest : IClassFixture<DockerFixture>
1717
private readonly DockerFixture _dockerFixture;
1818

1919
private readonly CalculationInstanceDockerAdapter _calculationInstanceDockerAdapter;
20-
private const int TestPort = 8080;
20+
private readonly int _testPort;
2121

2222
public CalculationInstanceDockerAdapterTest(
2323
DockerFixture dockerFixture,
@@ -32,7 +32,8 @@ ITestOutputHelper outputHelper
3232
Network = DockerFixture.TestNetworkName,
3333
}
3434
);
35-
_freePortProvider.GetFreeTcpPort().Returns(TestPort);
35+
_testPort = new FreePortProvider().GetFreeTcpPort();
36+
_freePortProvider.GetFreeTcpPort().Returns(_testPort);
3637

3738
var loggerFactory = LoggerFactory.Create(builder =>
3839
builder.AddProvider(new XUnitLoggerProvider(outputHelper))
@@ -55,7 +56,7 @@ public async Task Allocate_ShouldCreateAndStartContainerAndReturnUrlWithNetworkI
5556
var containerUrl = await _calculationInstanceDockerAdapter.Allocate(instanceId, image);
5657

5758
containerUrl.ShouldStartWith("http://");
58-
containerUrl.ShouldContain($":{TestPort}");
59+
containerUrl.ShouldContain($":{_testPort}");
5960
var containers = await _dockerFixture.DockerClient.Containers.ListContainersAsync(
6061
new ContainersListParameters { All = true }
6162
);
@@ -69,7 +70,7 @@ public async Task Allocate_ShouldCreateAndStartContainerAndReturnUrlWithNetworkI
6970
.NetworkSettings.Networks[DockerFixture.TestNetworkName]
7071
.IPAddress.ShouldNotBeNullOrEmpty();
7172
containerUrl.ShouldBe(
72-
$"http://{ourContainer.NetworkSettings.Networks[DockerFixture.TestNetworkName].IPAddress}:{TestPort}"
73+
$"http://{ourContainer.NetworkSettings.Networks[DockerFixture.TestNetworkName].IPAddress}:{_testPort}"
7374
);
7475
}
7576

0 commit comments

Comments
 (0)