Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I publish a random port? #678

Open
baautitz opened this issue May 17, 2024 · 1 comment
Open

How do I publish a random port? #678

baautitz opened this issue May 17, 2024 · 1 comment

Comments

@baautitz
Copy link

I'm attempting to dynamically assign a random port to a container upon its creation. I've been experimenting with various configurations, including setting the PortBindings property to null and trying different values for HostPort such as 0, null, an empty string, and "0". However, I haven't been successful in achieving the desired outcome. Here's the snippet of code I've been working with:

CreateContainerParameters containerParameters = new CreateContainerParameters {
    Name = name,
    Image = type,
    HostConfig = new HostConfig {
        PortBindings = new Dictionary<string, IList<PortBinding>> {
            { "25565/tcp", [] }
        }
    }
};
@hakimdotdev
Copy link

hakimdotdev commented May 21, 2024

Hi, this works for me:

var portBindings = new Dictionary<string, IList<PortBinding>>();
var exposedPorts = new Dictionary<string, EmptyStruct>();
portBindings.Add($"{hostport}/tcp", new List<PortBinding> { new() { HostPort = port.ToString() } });
exposedPorts.Add($"{port}/tcp", default);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants