@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(); }
try { if (serialized.isEmpty()) { return Binding.empty(); switch (parts.length) { case 2: { return new Binding(parts[0], parts[1]); return parts[0].contains(".") ? Binding.bindIp(parts[0]) : Binding.bindPortSpec(parts[0]);
portBindings.add(new PortBinding(Ports.Binding.bindPortSpec(""+hostPort), new ExposedPort(containerPort, InternetProtocol.TCP)));
microservice.getPortMappings().forEach(mapping -> { ExposedPort internal = ExposedPort.tcp(mapping.getInside()); Binding external = Binding.bindPort(mapping.getOutside()); portBindings.bind(internal, external); exposedPorts.add(internal);
Integer mappedPort = portMappings.get(exposedPort.getPort()); if (mappedPort != null) { portBindings.bind(exposedPort, Ports.Binding.bindPort(mappedPort));
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; }
String ipAddress = b.getHostIp(); if (ipAddress != null && !"0.0.0.0".equals(ipAddress)) { return ipAddress;
try { if (serialized.isEmpty()) { return Binding.empty(); switch (parts.length) { case 2: { return new Binding(parts[0], parts[1]); return parts[0].contains(".") ? Binding.bindIp(parts[0]) : Binding.bindPortSpec(parts[0]);
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); }
@Override public Ports deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { Ports out = new Ports(); ObjectCodec oc = jsonParser.getCodec(); JsonNode node = oc.readTree(jsonParser); for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext();) { Map.Entry<String, JsonNode> portNode = it.next(); JsonNode bindingsArray = portNode.getValue(); if (bindingsArray.equals(NullNode.getInstance())) { out.bind(ExposedPort.parse(portNode.getKey()), null); } else { for (int i = 0; i < bindingsArray.size(); i++) { JsonNode bindingNode = bindingsArray.get(i); if (!bindingNode.equals(NullNode.getInstance())) { String hostIp = bindingNode.get("HostIp").textValue(); String hostPort = bindingNode.get("HostPort").textValue(); out.bind(ExposedPort.parse(portNode.getKey()), new Binding(hostIp, hostPort)); } } } } return out; } }
/** * 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(); }
@Override protected CreateContainerCmd dockerCommand() { int port = getServicePort(); ExposedPort exposedPort = ExposedPort.tcp(port); Ports portBindings = new Ports(); if (getCluster().isMapPortsToHost()) { portBindings.bind(exposedPort, Ports.Binding.bindPort(port)); } CreateContainerCmd cmd = DockerClientFactory.build().createContainerCmd(getImageName() + ":" + getImageTag()) .withName(getName()) .withExposedPorts(new ExposedPort(getServicePort())) .withEnv(newEnvironment() .withValues(getMesosMasterEnvVars()) .withValues(getSharedEnvVars()) .createEnvironment()) .withPortBindings(portBindings); MesosDns mesosDns = getCluster().getMesosDns(); if (mesosDns != null) { cmd.withDns(mesosDns.getIpAddress()); } return cmd; }
private void startContainer(Map<String, String> env, Map<Integer, Integer> ports, Map<String, String> cmd, Consumer<String> log) { final CreateContainerResponse container = client.createContainerCmd(image) .withCmd(cmd.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(toList())) .withLabels(map(label, "true")) .withEnv(env.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(toList())) .withPortBindings(ports.entrySet().stream().map(e -> new PortBinding(Ports.Binding.bindPort(e.getKey()), ExposedPort.tcp(e.getValue()))).collect(toList())) .exec(); client.startContainerCmd(container.getId()).exec(); client.logContainerCmd(container.getId()).withFollowStream(true).withStdOut(true).withStdErr(true).exec( new LogContainerResultCallback() { public void onNext(Frame frame) { final String msg = new String(frame.getPayload()); log.accept(msg.endsWith("\n") ? msg.substring(0, msg.length() - 1) : msg); } }); } }
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"); } }