Skip to content

Commit

Permalink
Merge pull request #204 from 0xced/ExposeAllPorts
Browse files Browse the repository at this point in the history
Add new ExposeAllPorts on the ContainerBuilder
  • Loading branch information
mariotoffia authored Sep 14, 2021
2 parents 35624b6 + 94ffbcf commit c690224
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 c690224

Please sign in to comment.