diff --git a/src/Core/ContainerPortMapping.cs b/src/Core/ContainerPortMapping.cs index cf46d27..9c63a44 100644 --- a/src/Core/ContainerPortMapping.cs +++ b/src/Core/ContainerPortMapping.cs @@ -27,5 +27,12 @@ public class ContainerPortMapping /// External port that value will be resolved from variable /// public string ExternalPortVariableName { get; internal set; } + + /// + /// Allowed host IP. Restriction + /// Example: HostIp = 127.0.0.1 + /// Default all IPs are allowed + /// + public string? HostIp { get; internal set; } } } diff --git a/src/Core/ContainerResourceBuilder.cs b/src/Core/ContainerResourceBuilder.cs index 5ec513d..c1a3a59 100644 --- a/src/Core/ContainerResourceBuilder.cs +++ b/src/Core/ContainerResourceBuilder.cs @@ -135,6 +135,18 @@ public ContainerResourceBuilder Password(string password) return this; } + /// + /// Sets the allowed host IP address to use the external port. + /// Per default all IPs are allowed (0.0.0.0). + /// Example usage: if only localhost shall be allowed, then use 127.0.0.1 + /// + /// The allowed host IP for which the external port is exposed. + public ContainerResourceBuilder HostIp(string? hostIp) + { + _options.HostIp = hostIp; + return this; + } + /// /// Sets the main internal port of this container to the given value. /// @@ -182,14 +194,21 @@ public ContainerResourceBuilder ExternalPort(int port) /// Only provide an external port if a static external port is required. /// When the given external port is already in use by a container, the creation will fail. /// + /// + /// Allowed host IP. Default all IPs are allowed + /// /// - public ContainerResourceBuilder AddPortMapping(int internalPort, int externalPort = 0) + public ContainerResourceBuilder AddPortMapping( + int internalPort, + int externalPort = 0, + string? hostIp = null) { _options.AdditionalPortMappings.Add( new ContainerPortMapping() { ExternalPort = externalPort, - InternalPort = internalPort + InternalPort = internalPort, + HostIp = hostIp }); return this; } @@ -208,16 +227,21 @@ public ContainerResourceBuilder AddPortMapping(int internalPort, int externalPor /// Only provide an external port if a static external port is required. /// When the given external port is already in use by a container, the creation will fail. /// + /// + /// Allowed host IP. Default all IPs are allowed + /// /// public ContainerResourceBuilder AddPortMapping( int internalPort, - string externalPortVariableName) + string externalPortVariableName, + string? hostIp = null) { _options.AdditionalPortMappings.Add( new ContainerPortMapping() { InternalPort = internalPort, ExternalPortVariableName = externalPortVariableName, + HostIp = hostIp }); return this; } @@ -239,16 +263,21 @@ public ContainerResourceBuilder AddPortMapping( /// Only provide an external port if a static external port is required. /// When the given external port is already in use by a container, the creation will fail. /// + /// + /// Allowed host IP. Default all IPs are allowed + /// /// public ContainerResourceBuilder AddPortMapping( string internalPortVariableName, - int externalPort = 0) + int externalPort = 0, + string? hostIp = null) { _options.AdditionalPortMappings.Add( new ContainerPortMapping() { InternalPortVariableName = internalPortVariableName, ExternalPort = externalPort, + HostIp = hostIp }); return this; } @@ -268,16 +297,21 @@ public ContainerResourceBuilder AddPortMapping( /// Only provide an external port if a static external port is required. /// When the given external port is already in use by a container, the creation will fail. /// + /// + /// Allowed host IP. Default all IPs are allowed + /// /// public ContainerResourceBuilder AddPortMapping( string internalPortVariableName, - string externalPortVariableName) + string externalPortVariableName, + string? hostIp = null) { _options.AdditionalPortMappings.Add( new ContainerPortMapping() { InternalPortVariableName = internalPortVariableName, - ExternalPortVariableName = externalPortVariableName + ExternalPortVariableName = externalPortVariableName, + HostIp = hostIp }); return this; } diff --git a/src/Core/ContainerResourceSettings.cs b/src/Core/ContainerResourceSettings.cs index 5926a99..d658df3 100644 --- a/src/Core/ContainerResourceSettings.cs +++ b/src/Core/ContainerResourceSettings.cs @@ -18,6 +18,13 @@ public class ContainerResourceSettings /// public string Image { get; internal set; } + /// + /// Allowed host IP. Restriction + /// Example: HostIp = 127.0.0.1 + /// Default all IPs are allowed + /// + public string? HostIp { get; internal set; } + /// /// Returns the main internal port of the container /// diff --git a/src/Core/DockerContainerManager.cs b/src/Core/DockerContainerManager.cs index eebf843..f3d624c 100644 --- a/src/Core/DockerContainerManager.cs +++ b/src/Core/DockerContainerManager.cs @@ -302,6 +302,7 @@ private async Task CreateContainerAsync() { InternalPort = _settings.InternalPort, ExternalPort = _settings.ExternalPort, + HostIp = _settings.HostIp } }; allPorts.AddRange(_settings.AdditionalPortMappings); @@ -316,7 +317,7 @@ private async Task CreateContainerAsync() portMapping.Value.Add( new PortBinding() { - HostIP = "", + HostIP = containerPortMapping.HostIp ?? "", HostPort = containerPortMapping.ExternalPort != 0 ? containerPortMapping.ExternalPort.ToString() : "" @@ -489,11 +490,12 @@ private async Task ResolveHostAddressAsync() { Instance.HostPort = ResolvePort(inspectResponse, $"{_settings.InternalPort}/tcp"); - foreach (ContainerPortMapping portMapping - in _settings.AdditionalPortMappings) + + foreach (ContainerPortMapping portMapping in _settings.AdditionalPortMappings) { Instance.AdditionalPorts.Add(new ContainerPortMapping() { + HostIp = portMapping.HostIp, InternalPort = portMapping.InternalPort, ExternalPort = ResolvePort( inspectResponse,