Optional<ContainerNetwork> getNetwork() { return Optional.ofNullable(container) .map(GenericContainer::getContainerInfo) .flatMap(it -> it.getNetworkSettings().getNetworks().values().stream().findFirst()); } }
/** * 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(); }
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()); } }
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()); } }
@SuppressWarnings("deprecation") public String getContainerIp() { return container.getContainerInfo().getNetworkSettings().getIpAddress(); }
public Map<ExposedPort, Binding[]> getPortBindings() { return containerInfo.getNetworkSettings().getPorts().getBindings(); }
public boolean hasPortBindings() { Ports ports = containerInfo.getNetworkSettings().getPorts(); return ((ports != null) && (ports.getBindings() != null) && (!ports.getBindings().isEmpty())); }
private int getMappedPort(int exposed) { return Integer.parseInt(containerInfo.getNetworkSettings().getPorts().getBindings().get(new ExposedPort(exposed))[0].getHostPortSpec()); }
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"); } }
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"); } }
protected String getContainerHost() { if (OSUtils.isMac()) { return "localhost"; } String containerNetwork = environment.getProperty("embedded.container.container-network", "bridge"); return containerInfo.getNetworkSettings().getNetworks().get(containerNetwork).getIpAddress(); }
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); }
/** * 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; } }
/** * @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(); }
/** * @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(); }
private NetworkSettings getNetworkSettings(Container container, DockerFacade dockerClient) { InspectContainerResponse info = dockerClient.containers().inspectContainer(container.getId()).execute(); return info.getNetworkSettings(); }
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"); } }
@SuppressWarnings("deprecation") public String getContainerIp() { return container.getContainerInfo().getNetworkSettings().getIpAddress(); }