Skip to content
This repository has been archived by the owner on Nov 6, 2018. It is now read-only.

Publis one port with Random host port #69

Open
Khazrak opened this issue Aug 4, 2016 · 1 comment
Open

Publis one port with Random host port #69

Khazrak opened this issue Aug 4, 2016 · 1 comment

Comments

@Khazrak
Copy link
Contributor

Khazrak commented Aug 4, 2016

The client can host specific ports and publish all exposed ports with random ports.

But if you want to publish just on port with random host port, this doesn't seem to work.
the docker command would be "docker run -d -p 8080 " then 8080 would be random.

If I interpret the Remote API then a empty String should do it, but it didn't work with the PortBinding as it is now. Maybe empty string isn't sent and it sees it as null?

@m0schaefer
Copy link

Worked for me:

Map<String, ArrayList> portBinding = new HashMap<>();

ArrayList hostPort = new ArrayList<>();
PortBinding portBindingHttp = new PortBinding();
portBindingHttp.setHostPort("");
hostPort.add(portBindingHttp);
portBinding.put("8080/tcp", hostPort);

HostConfig hostConfig = new HostConfig();
hostConfig.setPortBindings(portBinding);

client.container().createNew()
.withImage(imageName)
.withName(containerName)
.withHostConfig(hostConfig)
...

Map<String, ArrayList> portMapping = client.container().withName(container.getId()).inspect().getNetworkSettings().getPorts();
String port = portMapping.get("8080/tcp").stream().findFirst().get().getHostPort();

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

No branches or pull requests

2 participants