/** * @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 static PortBinding createFromSubstrings(String binding, String exposedPort) throws IllegalArgumentException { return new PortBinding(Binding.parse(binding), ExposedPort.parse(exposedPort)); }
public static PortBinding parse(String serialized) throws IllegalArgumentException { try { String[] parts = StringUtils.splitByWholeSeparator(serialized, ":"); switch (parts.length) { case 3: // 127.0.0.1:80:8080/tcp return createFromSubstrings(parts[0] + ":" + parts[1], parts[2]); case 2: // 80:8080 // 127.0.0.1::8080 return createFromSubstrings(parts[0], parts[1]); case 1: // 8080 return createFromSubstrings("", parts[0]); default: throw new IllegalArgumentException(); } } catch (Exception e) { throw new IllegalArgumentException("Error parsing PortBinding '" + serialized + "'", e); } }
/** * Adds the specified {@link PortBinding}(s) to the list of {@link PortBinding}s. */ public void add(PortBinding... portBindings) { for (PortBinding binding : portBindings) { bind(binding.getExposedPort(), binding.getBinding()); } }
@Nullable @Override public PortBinding apply(String s) { return PortBinding.parse(s); } });
private static PortBinding parseOnePortBinding(String definition) throws IllegalArgumentException { try { return PortBinding.parse(definition.replace(' ', ':')); } catch (Exception e) { throw new IllegalArgumentException("Port binding needs to be in format '[hostIP:]hostPort containerPort[/protocol]'"); } }
@Override public boolean equals(Object obj) { if (obj instanceof PortBinding) { PortBinding other = (PortBinding) obj; return new EqualsBuilder().append(binding, other.getBinding()).append(exposedPort, other.getExposedPort()) .isEquals(); } else { return super.equals(obj); } }
@Override public void beforeContainerCreated(DockerAPI api, String workdir, CreateContainerCmd cmd) throws IOException, InterruptedException { // TODO define a strategy for SSHD process configuration so we support more than openssh's sshd if (cmd.getCmd() == null || cmd.getCmd().length == 0) { if (sshKeyStrategy.getInjectedKey() != null) { cmd.withCmd("/usr/sbin/sshd", "-D", "-p", String.valueOf(port), // override sshd_config to force retrieval of InstanceIdentity public for as authentication "-o", "AuthorizedKeysCommand=/root/authorized_key", "-o", "AuthorizedKeysCommandUser=root" ); } else { cmd.withCmd("/usr/sbin/sshd", "-D", "-p", String.valueOf(port)); } } cmd.withPortSpecs(port+"/tcp"); final PortBinding sshPortBinding = PortBinding.parse(":" + port); final Ports portBindings = cmd.getPortBindings(); if(portBindings != null) { portBindings.add(sshPortBinding); cmd.withPortBindings(portBindings); } else { cmd.withPortBindings(sshPortBinding); } cmd.withExposedPorts(ExposedPort.parse(port+"/tcp")); }
/** * @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 static PortBinding createFromSubstrings(String binding, String exposedPort) throws IllegalArgumentException { return new PortBinding(Binding.parse(binding), ExposedPort.parse(exposedPort)); }
/** * Adds the specified {@link PortBinding}(s) to the list of {@link PortBinding}s. */ public void add(PortBinding... portBindings) { for (PortBinding binding : portBindings) { bind(binding.getExposedPort(), binding.getBinding()); } }
public static PortBinding parse(String serialized) throws IllegalArgumentException { try { String[] parts = StringUtils.splitByWholeSeparator(serialized, ":"); switch (parts.length) { case 3: // 127.0.0.1:80:8080/tcp return createFromSubstrings(parts[0] + ":" + parts[1], parts[2]); case 2: // 80:8080 // 127.0.0.1::8080 return createFromSubstrings(parts[0], parts[1]); case 1: // 8080 return createFromSubstrings("", parts[0]); default: throw new IllegalArgumentException(); } } catch (Exception e) { throw new IllegalArgumentException("Error parsing PortBinding '" + serialized + "'", e); } }
private GenericContainer<?> getContainer(String image, int port, Network network, String logWaitRegex, int logWaitTimes, boolean matchExposedPort) { GenericContainer<?> container = new GenericContainer<>(image) .withExposedPorts(port) .waitingFor( Wait.forLogMessage(logWaitRegex, logWaitTimes).withStartupTimeout(Duration.ofMinutes(5L)) ); if (network != null) { container.withNetwork(network); } if(matchExposedPort) { container.withCreateContainerCmdModifier( command -> command.withPortBindings(PortBinding.parse(String.format("%d:%d", port, port))) ); } return container; }
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); } }); } }
@Override public boolean equals(Object obj) { if (obj instanceof PortBinding) { PortBinding other = (PortBinding) obj; return new EqualsBuilder().append(binding, other.getBinding()).append(exposedPort, other.getExposedPort()) .isEquals(); } else { return super.equals(obj); } }
@Test public void testWithAContainerWithLabels() throws InterruptedException { Map<String, String> labels = new LinkedHashMap<>(); labels.put("service.type", "http-endpoint"); labels.put("ssl", "true"); CreateContainerResponse container = client.createContainerCmd("nginx") .withExposedPorts(ExposedPort.tcp(80), ExposedPort.tcp(443)) .withPortBindings(PortBinding.parse("80")) .withLabels(labels) .exec(); AtomicBoolean done = new AtomicBoolean(); Future<Void> future = Future.future(); future.setHandler(ar -> done.set(ar.succeeded())); bridge.scan(future); await().untilAtomic(done, is(true)); assertThat(bridge.getServices()).hasSize(0); done.set(false); client.startContainerCmd(container.getId()).exec(); Future<Void> future2 = Future.future(); future2.setHandler(ar -> done.set(ar.succeeded())); bridge.scan(future2); await().untilAtomic(done, is(true)); assertThat(bridge.getServices()).hasSize(1); DockerService service = bridge.getServices().get(0); assertThat(service.records()).hasSize(1); assertThat(service.records().get(0).getLocation().getString("endpoint")).startsWith("https"); }
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; }
@Test public void testWithAContainerWithLabels() throws InterruptedException { Map<String, String> labels = new LinkedHashMap<>(); labels.put("service.type", "http-endpoint"); labels.put("ssl", "true"); CreateContainerResponse container = client.createContainerCmd("nginx") .withExposedPorts(ExposedPort.tcp(80), ExposedPort.tcp(443)) .withPortBindings(PortBinding.parse("80")) .withLabels(labels) .exec(); AtomicBoolean done = new AtomicBoolean(); Future<Void> future = Future.future(); future.setHandler(ar -> done.set(ar.succeeded())); bridge.scan(future); await().untilAtomic(done, is(true)); assertThat(bridge.getServices()).hasSize(0); done.set(false); client.startContainerCmd(container.getId()).exec(); Future<Void> future2 = Future.future(); future2.setHandler(ar -> done.set(ar.succeeded())); bridge.scan(future2); await().untilAtomic(done, is(true)); assertThat(bridge.getServices()).hasSize(1); DockerService service = bridge.getServices().get(0); assertThat(service.records()).hasSize(1); assertThat(service.records().get(0).getLocation().getString("endpoint")).startsWith("https"); }
portBindings.add(new PortBinding(Ports.Binding.bindPortSpec(""+hostPort), new ExposedPort(containerPort, InternetProtocol.TCP)));
@Test public void testWithAContainerWithAPort() throws InterruptedException { CreateContainerResponse container = client.createContainerCmd("nginx") .withExposedPorts(ExposedPort.tcp(80), ExposedPort.tcp(443)) .withPortBindings(PortBinding.parse("80")) .exec(); AtomicBoolean done = new AtomicBoolean(); Future<Void> future = Future.future(); future.setHandler(ar -> done.set(ar.succeeded())); bridge.scan(future); await().untilAtomic(done, is(true)); assertThat(bridge.getServices()).hasSize(0); done.set(false); client.startContainerCmd(container.getId()).exec(); Future<Void> future2 = Future.future(); future2.setHandler(ar -> done.set(ar.succeeded())); bridge.scan(future2); await().untilAtomic(done, is(true)); assertThat(bridge.getServices()).hasSize(1); DockerService service = bridge.getServices().get(0); assertThat(service.records()).hasSize(1); }