Java Code Examples for com.github.dockerjava.api.model.Ports#getBindings()
The following examples show how to use
com.github.dockerjava.api.model.Ports#getBindings() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DockerComputerSSHConnector.java From docker-plugin with MIT License | 6 votes |
private static InetSocketAddress getBindingForPort(DockerAPI api, InspectContainerResponse ir, int internalPort) { // get exposed port ExposedPort sshPort = new ExposedPort(internalPort); Integer port = 22; final NetworkSettings networkSettings = ir.getNetworkSettings(); final Ports ports = networkSettings.getPorts(); final Map<ExposedPort, Ports.Binding[]> bindings = ports.getBindings(); // Get the binding that goes to the port that we're interested in (e.g: 22) final Ports.Binding[] sshBindings = bindings.get(sshPort); // Find where it's mapped to for (Ports.Binding b : sshBindings) { String hps = b.getHostPortSpec(); port = Integer.valueOf(hps); } String host = getExternalIP(api, ir, networkSettings, sshBindings); return new InetSocketAddress(host, port); }
Example 2
Source File: DockerComputerSSHConnectorTest.java From docker-plugin with MIT License | 6 votes |
@Test public void testPortBinding() throws IOException, InterruptedException { DockerComputerSSHConnector connector = new DockerComputerSSHConnector(Mockito.mock(DockerComputerSSHConnector.SSHKeyStrategy.class)); CreateContainerCmdImpl cmd = new CreateContainerCmdImpl(Mockito.mock(CreateContainerCmd.Exec.class), Mockito.mock(AuthConfig.class), ""); cmd.withPortBindings(PortBinding.parse("42:42")); connector.setPort(22); connector.beforeContainerCreated(Mockito.mock(DockerAPI.class), "/workdir", cmd); final Ports portBindings = cmd.getPortBindings(); Assert.assertNotNull(portBindings); final Map<ExposedPort, Ports.Binding[]> bindingMap = portBindings.getBindings(); Assert.assertNotNull(bindingMap); Assert.assertEquals(2, bindingMap.size()); final Ports.Binding[] configuredBindings = bindingMap.get(new ExposedPort(42)); Assert.assertNotNull(configuredBindings); Assert.assertEquals(1, configuredBindings.length); Assert.assertEquals("42", configuredBindings[0].getHostPortSpec()); final Ports.Binding[] sshBindings = bindingMap.get(new ExposedPort(22)); Assert.assertNotNull(sshBindings); Assert.assertEquals(1, sshBindings.length); Assert.assertNull(sshBindings[0].getHostPortSpec()); System.out.println(); }
Example 3
Source File: DefaultDockerClientIT.java From junit5-docker with Apache License 2.0 | 5 votes |
@Test @DisplayName("start a container with one port") public void shouldStartContainerWithOnePort() { String containerId = defaultDockerClient.startContainer(WANTED_IMAGE, emptyMap(), new PortBinding(8081, 8080)); InspectContainerResponse startedContainer = dockerClient.inspectContainerCmd(containerId).exec(); Ports ports = startedContainer.getHostConfig().getPortBindings(); assertThat(ports).isNotNull(); Map<ExposedPort, Ports.Binding[]> portBindings = ports.getBindings(); assertThat(portBindings).hasSize(1) .containsKeys(new ExposedPort(8080)); assertThat(portBindings.get(new ExposedPort(8080))).hasSize(1) .extracting(Ports.Binding::getHostPortSpec) .contains("8081"); }