Skip to content

[Enhancement]: Add API to enumerate all port mappings of a IContainer #1477

Description

@Dave-Senn

Problem

For a lookup of a mapped port using GetMappedPublicPort knowledge of a specific port is required.
If there would be a Method like {I}Dictionary<Int32, Int32> GetMapped{Public}Ports it would be easier to write generic code for IContainer.

Example:
We use a certain default port range for our services.
When waiting for a container to start, we could have a generic IWaitUntil making a request to a known "alive" endpoint using a port returned by the new GetMapped{Public}Ports method.

public sealed class WaitUntilAlive : IWaitUntil
{
    public async Task<Boolean> UntilAsync( IContainer container )
    {
        IDictionary<Int32, Int32> ports = container.GetMappedPublicPorts();
        var alive = true;
        foreach ( var (internalPort, externalPort) in ports )
        {
            if ( internalPort is < 48_000 or > 48_100 )
                continue;

            var requestUri = new UriBuilder( Uri.UriSchemeHttp, container.Hostname, externalPort, "/alive" ).Uri;
            using var httpClient = new HttpClient();
            var response = await httpClient.GetAsync( requestUri );
            alive &= response.IsSuccessStatusCode && alive;
        }

        return alive;
    }
}

Solution

Adding new method {I}Dictionary<Int32, Int32> GetMapped{Public}Ports to IContainer.
Ports could be tracked when calling WithPortBinding.

Benefit

The new API would make it easier to get "metadata" from a container without too much knowledge about the specific container.

Alternatives

No new API.

Would you like to help contributing this enhancement?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions