Skip to content

Commit

Permalink
Add new ExposeAllPorts on the ContainerBuilder
Browse files Browse the repository at this point in the history
This turns `PublishAllPorts` to `true` and also ensures that `ExposePort` and `ExposeAllPorts` are not used together since they are mutually exclusive anyway.
  • Loading branch information
0xced committed Sep 13, 2021
1 parent 1057a1f commit 94ffbcf
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Ductus.FluentDocker/Builders/ContainerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ public ContainerBuilder UseDnsOption(params string[] option)

public ContainerBuilder ExposePort(int hostPort, int containerPort, string protocol = "tcp")
{
EnsurePublishAllPortsIsFalse();

if (string.IsNullOrEmpty(protocol))
{
protocol = "tcp";
Expand All @@ -293,10 +295,38 @@ public ContainerBuilder ExposePort(int hostPort, int containerPort, string proto

public ContainerBuilder ExposePort(int containerPort)
{
EnsurePublishAllPortsIsFalse();

_config.CreateParams.PortMappings = _config.CreateParams.PortMappings.ArrayAdd($"{containerPort}");
return this;
}

public ContainerBuilder ExposeAllPorts()
{
EnsurePortMappingsIsEmpty();

_config.CreateParams.PublishAllPorts = true;
return this;
}

private void EnsurePublishAllPortsIsFalse()
{
if (_config.CreateParams.PublishAllPorts)
{
throw new FluentDockerNotSupportedException($"{nameof(ExposePort)} is mutually exclusive with {nameof(ExposeAllPorts)} methods. " +
$"Do not call {nameof(ExposeAllPorts)} if you want to explicitly expose ports.");
}
}

private void EnsurePortMappingsIsEmpty()
{
if (_config.CreateParams.PortMappings.Any())
{
throw new FluentDockerNotSupportedException($"{nameof(ExposeAllPorts)} is mutually exclusive with {nameof(ExposePort)} methods. " +
$"Do not call {nameof(ExposePort)} if you want to expose all ports.");
}
}

/// <summary>
/// Deprecated ue <see cref="UseHealthCheck(string, string, string, int)"/> instead.
/// </summary>
Expand Down

0 comments on commit 94ffbcf

Please sign in to comment.