@Override public boolean equals(Object obj) { if (obj instanceof Binding) { Binding other = (Binding) obj; return new EqualsBuilder().append(hostIp, other.getHostIp()).append(hostPortSpec, other.getHostPortSpec()) .isEquals(); } else { return super.equals(obj); } } }
/** * @return the port on which to check if the container is ready * @deprecated see {@link GenericContainer#getLivenessCheckPorts()} for replacement */ @Deprecated protected Integer getLivenessCheckPort() { // legacy implementation for backwards compatibility if (exposedPorts.size() > 0) { return getMappedPort(exposedPorts.get(0)); } else if (portBindings.size() > 0) { return Integer.valueOf(PortBinding.parse(portBindings.get(0)).getBinding().getHostPortSpec()); } else { return null; } }
@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(); }
URI publicUri = UriUtils.buildUri(UriUtils.HTTP_SCHEME, binding.getHostIp(), Integer.valueOf(binding.getHostPortSpec()), null, null, null);
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; }
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);
private void exportPortBindings(EnvVars envs, Map<ExposedPort, Binding[]> bindings) { StringBuilder ports = new StringBuilder(); for (ExposedPort exposedPort : bindings.keySet()) { ports.append(exposedPort.toString()).append(ID_SEPARATOR); Binding[] exposedPortBinding = bindings.get(exposedPort); if (exposedPortBinding == null) { continue; } envs.put(PORT_BINDING_PREFIX + exposedPort.getProtocol().name() + "_" + exposedPort.getPort(), exposedPortBinding[0].getHostPortSpec()); StringBuilder portBinding = new StringBuilder(); String hostIp = exposedPortBinding[0].getHostIp(); if (hostIp != null && hostIp.length() > 0) { portBinding.append(hostIp).append(":"); portBinding.append(exposedPortBinding[0].getHostPortSpec()); envs.put(HOST_SOCKET_PREFIX + exposedPort.getProtocol().name() + "_" + exposedPort.getPort(), portBinding.toString()); } } String bindPorts = ports.substring(0, ports.length() - 1).toString(); envs.put(PORT_BINDINGS_ENV_VAR, bindPorts); }
/** * Queries Docker to find the port bindings for the specified port and then performs the provided * {@code waitFunction}. * * @param containerPort the container port to decode and test against. * @param dockerClient the docker client to use. * @param container the container to test against. * @param waitFunction the wait function to use. * @return a {@link WaitResult}. */ public WaitResult wait(int containerPort, DockerFacade dockerClient, Container container, BiFunction<String, Integer, WaitResult> waitFunction) { for (Binding portBinding : getPortBindings(containerPort, container, dockerClient)) { String ipAddress = portBinding.getHostIp(); if (ipAddress.equals(DEFAULT_BINDING)) { ipAddress = dockerClient.hostInfo().getIpAddress(); } int externalPort = Integer.valueOf(portBinding.getHostPortSpec()); LOG.info("Running wait against: [{}:{}]: [{}]: [{}]", ipAddress, externalPort, container.getName(), waitFunction.getClass().getName()); return waitFunction.apply(ipAddress, externalPort); } return WaitResult.success(); }
@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(); }
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"); } }
/** * @return the port on which to check if the container is ready * @deprecated see {@link GenericContainer#getLivenessCheckPorts()} for replacement */ @Deprecated protected Integer getLivenessCheckPort() { // legacy implementation for backwards compatibility if (exposedPorts.size() > 0) { return getMappedPort(exposedPorts.get(0)); } else if (portBindings.size() > 0) { return Integer.valueOf(PortBinding.parse(portBindings.get(0)).getBinding().getHostPortSpec()); } else { return null; } }
private int getMappedPort(int exposed) { return Integer.parseInt(containerInfo.getNetworkSettings().getPorts().getBindings().get(new ExposedPort(exposed))[0].getHostPortSpec()); }
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()); } }
@Override public boolean equals(Object obj) { if (obj instanceof Binding) { Binding other = (Binding) obj; return new EqualsBuilder().append(hostIp, other.getHostIp()).append(hostPortSpec, other.getHostPortSpec()) .isEquals(); } else { return super.equals(obj); } } }
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; } }
/** * 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"); } }