/** * 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"); } }
Optional<ContainerNetwork> getNetwork() { return Optional.ofNullable(container) .map(GenericContainer::getContainerInfo) .flatMap(it -> it.getNetworkSettings().getNetworks().values().stream().findFirst()); } }
public String getIpAddress() { return containerInfo.getNetworkSettings().getIpAddress(); }
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); ContainerNetwork cn = icr.getNetworkSettings().getNetworks().get(this.containerNetworkName); URI clusterUri = UriUtils.buildUri(UriUtils.HTTP_SCHEME, cn.getIpAddress(), this.containerExposedPort, null, null, null);
if (response.getNetworkSettings().getNetworks() == null || response.getNetworkSettings().getNetworks().isEmpty()) { ipAddress = response.getNetworkSettings().getIpAddress(); } else { if (StringUtils.isNotBlank(connection.getDockerNetworkName()) && response.getNetworkSettings().getNetworks().containsKey(connection.getDockerNetworkName())) { ipAddress = response.getNetworkSettings().getNetworks().get(connection.getDockerNetworkName()).getIpAddress(); } else if (response.getNetworkSettings().getNetworks().containsKey("bridge")) { ipAddress = response.getNetworkSettings().getNetworks().get("bridge").getIpAddress(); } else { ipAddress = response.getNetworkSettings().getNetworks().values().iterator().next().getIpAddress(); for (Map.Entry<java.lang.String, ContainerNetwork> networkEntry : response.getNetworkSettings().getNetworks().entrySet()) { ipAddresses.put(networkEntry.getKey(), networkEntry.getValue().getIpAddress());
/** * @return IP Address of the container's gateway (which would be docker0) */ public static String getGatewayIpAddress(String containerId) { InspectContainerResponse response = DockerClientFactory.build().inspectContainerCmd(containerId).exec(); return response.getNetworkSettings().getGateway(); }
@SuppressWarnings("deprecation") public String getContainerIp() { return container.getContainerInfo().getNetworkSettings().getIpAddress(); }
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); ContainerNetwork cn = icr.getNetworkSettings().getNetworks().get(this.containerNetworkName); URI clusterUri = UriUtils.buildUri(UriUtils.HTTP_SCHEME, cn.getIpAddress(), this.containerExposedPort, null, null, null);
return networkSettings.getIpAddress();
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()); } }
Optional<ContainerNetwork> getNetwork() { return Optional.ofNullable(container).map(GenericContainer::getContainerInfo).flatMap(it -> it.getNetworkSettings().getNetworks().values().stream().findFirst()); }
/** * gets IPv4 address of a {@link Container} * * @param id - id of {@link Container} * @return ip address */ public String getContainerIpAddress(String id) throws NotFoundException, NotModifiedException { try { InspectContainerResponse inspect = dockerClient.inspectContainerCmd(id).exec(); return inspect.getNetworkSettings().getIpAddress(); } catch (Exception exp) { logWarning(MODULE_NAME, exp.getMessage()); throw exp; } }
public Map<ExposedPort, Binding[]> getPortBindings() { return containerInfo.getNetworkSettings().getPorts().getBindings(); }
private void logNetworks(String containerId) { Map<String, ContainerNetwork> networks = getClient().inspectContainerCmd(containerId).exec().getNetworkSettings().getNetworks(); int networksSize = networks.size(); log.debug("There are {} network(s) in the container {}", networksSize, containerId); if (networksSize == 0) { return; } int i = 0; for (Entry<String, ContainerNetwork> network : networks.entrySet()) { log.debug("{}) {} -> {}", ++i, network.getKey(), network.getValue()); } }
/** * @param containerId id of the container to inspect * @return IP Address of the container */ public static String getIpAddress(String containerId) { InspectContainerResponse response = DockerClientFactory.build().inspectContainerCmd(containerId).exec(); return response.getNetworkSettings().getIpAddress(); }
private Binding[] getPortBindings(int containerPort, Container container, DockerFacade dockerClient) { NetworkSettings networkSettings = getNetworkSettings(container, dockerClient); return networkSettings.getPorts().getBindings().get(ExposedPort.tcp(containerPort)); }
private void logNetworks(String containerId) { Map<String, ContainerNetwork> networks = getClient().inspectContainerCmd(containerId).exec().getNetworkSettings().getNetworks(); int networksSize = networks.size(); log.debug("There are {} network(s) in the container {}", networksSize, containerId); if (networksSize == 0) { return; } int i = 0; for (Entry<String, ContainerNetwork> network : networks.entrySet()) { log.debug("{}) {} -> {}", ++i, network.getKey(), network.getValue()); } }
public String getIPAddress(Id id) { Container container = findContainer(id); if (container != null && repo.conf(id).isExposeContainerIp()) { InspectContainerResponse containerInspectResponse = docker.inspectContainerCmd(container.getId()).exec(); return containerInspectResponse.getNetworkSettings().getIpAddress(); } else { throw new IllegalArgumentException(id + " container IP address is not exposed"); } }
public boolean hasPortBindings() { Ports ports = containerInfo.getNetworkSettings().getPorts(); return ((ports != null) && (ports.getBindings() != null) && (!ports.getBindings().isEmpty())); }
public String getContainerIpAddress() { if (isRunningInContainer()) { String ipAddr = inspectContainer(getContainerName()).getNetworkSettings() .getNetworks().values().iterator().next().getIpAddress(); log.trace("Docker container IP address {}", ipAddr); return ipAddr; } else { throw new DockerClientException( "Can't obtain container ip address if not running in container"); } }