/** * @return the port bindings */ default List<String> getPortBindings() { List<String> portBindings = new ArrayList<>(); final Ports hostPortBindings = this.getContainerInfo().getHostConfig().getPortBindings(); for (Map.Entry<ExposedPort, Ports.Binding[]> binding : hostPortBindings.getBindings().entrySet()) { for (Ports.Binding portBinding : binding.getValue()) { portBindings.add(String.format("%s:%s", portBinding.toString(), binding.getKey())); } } return portBindings; }
@Override public void serialize(Ports portBindings, JsonGenerator jsonGen, SerializerProvider serProvider) throws IOException, JsonProcessingException { jsonGen.writeStartObject(); for (Entry<ExposedPort, Binding[]> entry : portBindings.getBindings().entrySet()) { jsonGen.writeFieldName(entry.getKey().toString()); if (entry.getValue() != null) { jsonGen.writeStartArray(); for (Binding binding : entry.getValue()) { jsonGen.writeStartObject(); jsonGen.writeStringField("HostIp", binding.getHostIp() == null ? "" : binding.getHostIp()); jsonGen.writeStringField("HostPort", binding.getHostPortSpec() == null ? "" : binding.getHostPortSpec()); jsonGen.writeEndObject(); } jsonGen.writeEndArray(); } else { jsonGen.writeNull(); } } jsonGen.writeEndObject(); }
/** * Get the actual mapped port for a given port exposed by the container. * * @param originalPort the original TCP port that is exposed * @return the port that the exposed port is mapped to, or null if it is not exposed */ default Integer getMappedPort(int originalPort) { Preconditions.checkState(this.getContainerId() != null, "Mapped port can only be obtained after the container is started"); Ports.Binding[] binding = new Ports.Binding[0]; final InspectContainerResponse containerInfo = this.getContainerInfo(); if (containerInfo != null) { binding = containerInfo.getNetworkSettings().getPorts().getBindings().get(new ExposedPort(originalPort)); } if (binding != null && binding.length > 0 && binding[0] != null) { return Integer.valueOf(binding[0].getHostPortSpec()); } else { throw new IllegalArgumentException("Requested port (" + originalPort + ") is not mapped"); } }
public boolean hasPortBindings() { Ports ports = containerInfo.getNetworkSettings().getPorts(); return ((ports != null) && (ports.getBindings() != null) && (!ports.getBindings().isEmpty())); }
private static Map<Integer, Integer> getMappedPorts(final Ports containerPorts) { final HashMap<Integer, Integer> ports = new HashMap<>(); for (final Map.Entry<ExposedPort, Ports.Binding[]> m : containerPorts.getBindings().entrySet()) { ports.put(m.getKey().getPort(), Integer.valueOf(m.getValue()[0].getHostPortSpec())); } return ports; } }
private List<PortMapping> getContainerPorts(InspectContainerResponse inspectInfo) { HostConfig hostConfig = inspectInfo.getHostConfig(); Ports ports = hostConfig.getPortBindings(); return ports.getBindings().entrySet().stream() .flatMap(entity -> { int exposedPort = entity.getKey().getPort(); return Arrays.stream(entity.getValue()) .map(Binding::getHostPortSpec) .map(hostPort -> new PortMapping(Integer.valueOf(hostPort), exposedPort)); }) .collect(Collectors.toList()); }
/** * @return the port bindings */ default List<String> getPortBindings() { List<String> portBindings = new ArrayList<>(); final Ports hostPortBindings = this.getContainerInfo().getHostConfig().getPortBindings(); for (Map.Entry<ExposedPort, Ports.Binding[]> binding : hostPortBindings.getBindings().entrySet()) { for (Ports.Binding portBinding : binding.getValue()) { portBindings.add(String.format("%s:%s", portBinding.toString(), binding.getKey())); } } return portBindings; }
private int getMappedPort(NetworkSettings networkSettings, int originalPort) { ExposedPort exposedPort = new ExposedPort(originalPort); Ports ports = networkSettings.getPorts(); Map<ExposedPort, Ports.Binding[]> bindings = ports.getBindings(); Ports.Binding[] binding = bindings.get(exposedPort); return Integer.valueOf(binding[0].getHostPortSpec()); } }
public Map<ExposedPort, Binding[]> getPortBindings() { return containerInfo.getNetworkSettings().getPorts().getBindings(); }
@Override public void serialize(Ports portBindings, JsonGenerator jsonGen, SerializerProvider serProvider) throws IOException, JsonProcessingException { jsonGen.writeStartObject(); for (Entry<ExposedPort, Binding[]> entry : portBindings.getBindings().entrySet()) { jsonGen.writeFieldName(entry.getKey().toString()); if (entry.getValue() != null) { jsonGen.writeStartArray(); for (Binding binding : entry.getValue()) { jsonGen.writeStartObject(); jsonGen.writeStringField("HostIp", binding.getHostIp() == null ? "" : binding.getHostIp()); jsonGen.writeStringField("HostPort", binding.getHostPortSpec() == null ? "" : binding.getHostPortSpec()); jsonGen.writeEndObject(); } jsonGen.writeEndArray(); } else { jsonGen.writeNull(); } } jsonGen.writeEndObject(); }
public ContainerAssert hasBindPorts(String... ports) { isNotNull(); List<ExposedPort> exposedPorts = new ArrayList<ExposedPort>(); for (String port : ports) { exposedPorts.add(ExposedPort.parse(port)); } ExposedPort[] myports = new ExposedPort[exposedPorts.size()]; exposedPorts.toArray(myports); assertThat(getHostConfig().getPortBindings().getBindings()) .overridingErrorMessage("%nExpecting:%n <%s>%nto contain:%n <%s>", getHostConfig().getPortBindings().getBindings().keySet(), Arrays.asList(ports)) .containsKeys(myports); return this; }
void setPorts(Instance instance, ExposedPort[] exposedPorts, Ports portBindings) { if (exposedPorts == null) { exposedPorts = new ExposedPort[0]; } List<String> ports = new ArrayList<String>(); for (ExposedPort ep : exposedPorts) { String port = ep.toString(); Binding[] bindings = portBindings == null || portBindings.getBindings() == null ? null : portBindings.getBindings().get(ep); if (bindings != null && bindings.length > 0) { for (Binding b : bindings) { // HostPort should really be a string, not an int. Somehow empty string becomes 0 if (b.getHostPort() != null && b.getHostPort() != 0) { String fullPort = b.getHostPort() + ":" + port; ports.add(fullPort); } else { ports.add(port); } } } } setField(instance, FIELD_PORTS, ports); }
private Binding[] getPortBindings(int containerPort, Container container, DockerFacade dockerClient) { NetworkSettings networkSettings = getNetworkSettings(container, dockerClient); return networkSettings.getPorts().getBindings().get(ExposedPort.tcp(containerPort)); }
private int getMappedPort(int exposed) { return Integer.parseInt(containerInfo.getNetworkSettings().getPorts().getBindings().get(new ExposedPort(exposed))[0].getHostPortSpec()); }
private 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); }
/** * Get the actual mapped port for a given port exposed by the container. * * @param originalPort the original TCP port that is exposed * @return the port that the exposed port is mapped to, or null if it is not exposed */ default Integer getMappedPort(int originalPort) { Preconditions.checkState(this.getContainerId() != null, "Mapped port can only be obtained after the container is started"); Ports.Binding[] binding = new Ports.Binding[0]; final InspectContainerResponse containerInfo = this.getContainerInfo(); if (containerInfo != null) { binding = containerInfo.getNetworkSettings().getPorts().getBindings().get(new ExposedPort(originalPort)); } if (binding != null && binding.length > 0 && binding[0] != null) { return Integer.valueOf(binding[0].getHostPortSpec()); } else { throw new IllegalArgumentException("Requested port (" + originalPort + ") is not mapped"); } }
public static Binding binding(DockerClientExecutor executor, String cubeId) { InspectContainerResponse inspectResponse = executor.getDockerClient().inspectContainerCmd(cubeId).exec(); String dockerIp = getDockerServerIp(executor); String inernalIp = null; NetworkSettings networkSettings = inspectResponse.getNetworkSettings(); if (networkSettings != null) { inernalIp = networkSettings.getIpAddress(); } Binding binding = new Binding(dockerIp, inernalIp); HostConfig hostConfig = inspectResponse.getHostConfig(); if (hostConfig.getPortBindings() != null) { for (Entry<ExposedPort, com.github.dockerjava.api.model.Ports.Binding[]> bind : hostConfig.getPortBindings() .getBindings().entrySet()) { com.github.dockerjava.api.model.Ports.Binding[] allBindings = bind.getValue(); for (com.github.dockerjava.api.model.Ports.Binding bindings : allBindings) { binding.addPortBinding(bind.getKey().getPort(), Integer.parseInt(bindings.getHostPortSpec())); } } } else { ContainerConfig connectionConfig = inspectResponse.getConfig(); final ExposedPort[] exposedPorts = connectionConfig.getExposedPorts(); if (exposedPorts != null) { for (ExposedPort port : exposedPorts) { binding.addPortBinding(port.getPort(), -1); } } } return binding; }
public static Binding binding(DockerClientExecutor executor, String cubeId) { InspectContainerResponse inspectResponse = executor.getDockerClient().inspectContainerCmd(cubeId).exec(); String dockerIp = getDockerServerIp(executor); String inernalIp = null; NetworkSettings networkSettings = inspectResponse.getNetworkSettings(); if (networkSettings != null) { inernalIp = networkSettings.getIpAddress(); } Binding binding = new Binding(dockerIp, inernalIp); HostConfig hostConfig = inspectResponse.getHostConfig(); if (hostConfig.getPortBindings() != null) { for (Entry<ExposedPort, com.github.dockerjava.api.model.Ports.Binding[]> bind : hostConfig.getPortBindings() .getBindings().entrySet()) { com.github.dockerjava.api.model.Ports.Binding[] allBindings = bind.getValue(); for (com.github.dockerjava.api.model.Ports.Binding bindings : allBindings) { binding.addPortBinding(bind.getKey().getPort(), Integer.parseInt(bindings.getHostPortSpec())); } } } else { ContainerConfig connectionConfig = inspectResponse.getConfig(); final ExposedPort[] exposedPorts = connectionConfig.getExposedPorts(); if (exposedPorts != null) { for (ExposedPort port : exposedPorts) { binding.addPortBinding(port.getPort(), -1); } } } return binding; }
@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"); }
public ContainerVerificationHost setUpLocalPeerHost(String id) throws Throwable { InspectContainerResponse icr = this.dockerClient.inspectContainerCmd(id).exec(); ExposedPort exposedPort = new ExposedPort(this.containerExposedPort, this.containerProtocol); Ports.Binding binding = icr.getNetworkSettings().getPorts().getBindings().get(exposedPort)[0]; URI publicUri = UriUtils.buildUri(UriUtils.HTTP_SCHEME, binding.getHostIp(), Integer.valueOf(binding.getHostPortSpec()), null, null, null);