/** * Creates an {@link ExposedPort} for {@link InternetProtocol#TCP}. This is a shortcut for * <code>new ExposedPort(port, {@link InternetProtocol#TCP})</code> */ public static ExposedPort tcp(int port) { return new ExposedPort(port, TCP); }
/** * Creates an {@link ExposedPort} for {@link InternetProtocol#UDP}. This is a shortcut for * <code>new ExposedPort(port, {@link InternetProtocol#UDP})</code> */ public static ExposedPort udp(int port) { return new ExposedPort(port, UDP); }
/** * Parses a textual port specification (as used by the Docker CLI) to an {@link ExposedPort}. * * @param serialized * the specification, e.g. <code>80/tcp</code> * @return an {@link ExposedPort} matching the specification * @throws IllegalArgumentException * if the specification cannot be parsed */ public static ExposedPort parse(String serialized) throws IllegalArgumentException { try { String[] parts = serialized.split("/"); switch (parts.length) { case 1: return new ExposedPort(Integer.parseInt(parts[0])); case 2: return new ExposedPort(Integer.parseInt(parts[0]), InternetProtocol.parse(parts[1])); default: throw new IllegalArgumentException(); } } catch (Exception e) { throw new IllegalArgumentException("Error parsing ExposedPort '" + serialized + "'"); } }
/** * 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"); } }
/** * Creates an {@link ExposedPort} for {@link InternetProtocol#TCP}. This is a shortcut for * <code>new ExposedPort(port, {@link InternetProtocol#TCP})</code> */ public static ExposedPort tcp(int port) { return new ExposedPort(port, TCP); }
/** * Creates an {@link ExposedPort} for {@link InternetProtocol#UDP}. This is a shortcut for * <code>new ExposedPort(port, {@link InternetProtocol#UDP})</code> */ public static ExposedPort udp(int port) { return new ExposedPort(port, UDP); }
/** * Parses a textual port specification (as used by the Docker CLI) to an {@link ExposedPort}. * * @param serialized * the specification, e.g. <code>80/tcp</code> * @return an {@link ExposedPort} matching the specification * @throws IllegalArgumentException * if the specification cannot be parsed */ public static ExposedPort parse(String serialized) throws IllegalArgumentException { try { String[] parts = serialized.split("/"); switch (parts.length) { case 1: return new ExposedPort(Integer.parseInt(parts[0])); case 2: return new ExposedPort(Integer.parseInt(parts[0]), InternetProtocol.parse(parts[1])); default: throw new IllegalArgumentException(); } } catch (Exception e) { throw new IllegalArgumentException("Error parsing ExposedPort '" + serialized + "'"); } }
@Override protected CreateContainerCmd dockerCommand() { ArrayList<ExposedPort> exposedPorts = new ArrayList<>(); exposedPorts.add(new ExposedPort(getServicePort())); ArrayList<Integer> resourcePorts = ResourceUtil.parsePorts(getResources()); for (Integer port : resourcePorts) { exposedPorts.add(new ExposedPort(port)); } return getBaseCommand() .withExposedPorts(exposedPorts.toArray(new ExposedPort[exposedPorts.size()])); }
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()); } }
private int getMappedPort(int exposed) { return Integer.parseInt(containerInfo.getNetworkSettings().getPorts().getBindings().get(new ExposedPort(exposed))[0].getHostPortSpec()); }
public String run(String image, int... ports) { String containerId = docker.createContainerCmd(image).exec().getId(); List<PortBinding> portBindings = new ArrayList<>(); if (ports != null) { for (int port : ports) { portBindings.add(new PortBinding(new Ports.Binding(port), new ExposedPort(port))); } } docker.startContainerCmd(containerId).withPortBindings(portBindings.toArray(new PortBinding[portBindings.size()])).exec(); startedContainers.add(containerId); registerShutdown(containerId); return containerId; }
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"); } }
@Override protected CreateContainerCmd dockerCommand() { return DockerClientFactory.build() .createContainerCmd(config.getImageName() + ":" + config.getImageTag()) .withEnv(newEnvironment() .withValues(getMesosDNSEnvVars()) .createEnvironment()) .withCmd("-v=2", "-config=/etc/mesos-dns/config.json") .withExposedPorts(new ExposedPort(Integer.valueOf(DNS_PORT), InternetProtocol.UDP), new ExposedPort(Integer.valueOf(DNS_PORT), InternetProtocol.TCP)) .withName(getName()); }
private Set<ExposedPort> resolveExposedPorts(CubeContainer containerConfiguration, CreateContainerCmd createContainerCmd) { Set<ExposedPort> allExposedPorts = new HashSet<>(); if (containerConfiguration.getPortBindings() != null) { for (PortBinding binding : containerConfiguration.getPortBindings()) { allExposedPorts.add(new ExposedPort(binding.getExposedPort().getExposed(), InternetProtocol.parse(binding.getExposedPort().getType()))); } } if (containerConfiguration.getExposedPorts() != null) { for (org.arquillian.cube.docker.impl.client.config.ExposedPort port : containerConfiguration.getExposedPorts()) { allExposedPorts.add(new ExposedPort(port.getExposed(), InternetProtocol.parse(port.getType()))); } } return allExposedPorts; }
private Set<ExposedPort> resolveExposedPorts(CubeContainer containerConfiguration, CreateContainerCmd createContainerCmd) { Set<ExposedPort> allExposedPorts = new HashSet<>(); if (containerConfiguration.getPortBindings() != null) { for (PortBinding binding : containerConfiguration.getPortBindings()) { allExposedPorts.add(new ExposedPort(binding.getExposedPort().getExposed(), InternetProtocol.parse(binding.getExposedPort().getType()))); } } if (containerConfiguration.getExposedPorts() != null) { for (org.arquillian.cube.docker.impl.client.config.ExposedPort port : containerConfiguration.getExposedPorts()) { allExposedPorts.add(new ExposedPort(port.getExposed(), InternetProtocol.parse(port.getType()))); } } return allExposedPorts; }
@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)); } return DockerClientFactory.build().createContainerCmd(config.getImageName() + ":" + config.getImageTag()) .withName(getName()) .withExposedPorts(new ExposedPort(ZooKeeperConfig.DEFAULT_ZOOKEEPER_PORT), new ExposedPort(2888), new ExposedPort(3888)) .withPortBindings(portBindings); }
private Ports toPortBindings(Collection<PortBinding> portBindings) { Ports ports = new Ports(); for (PortBinding portBinding : portBindings) { ports.bind( new ExposedPort( portBinding.getExposedPort().getExposed(), InternetProtocol.parse(portBinding.getExposedPort().getType())), new Binding(portBinding.getHost(), Integer.toString(portBinding.getBound()))); } return ports; }
private Ports toPortBindings(Collection<PortBinding> portBindings) { Ports ports = new Ports(); for (PortBinding portBinding : portBindings) { ports.bind( new ExposedPort( portBinding.getExposedPort().getExposed(), InternetProtocol.parse(portBinding.getExposedPort().getType())), new Binding(portBinding.getHost(), Integer.toString(portBinding.getBound()))); } return ports; }
@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"); }