/** * @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; } }
@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]'"); } }
/** * @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; } }
@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")); }
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; }
@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"); }
@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); }
@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"); }
@Test public void testWithAContainerWithTwoPorts() throws InterruptedException { CreateContainerResponse container = client.createContainerCmd("nginx") .withExposedPorts(ExposedPort.tcp(80), ExposedPort.tcp(443)) .withPortBindings(PortBinding.parse("80"), PortBinding.parse("443")) .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(2); client.stopContainerCmd(container.getId()).exec(); done.set(false); Future<Void> future3 = Future.future(); future3.setHandler(ar -> done.set(ar.succeeded())); bridge.scan(future3); await().untilAtomic(done, is(true)); assertThat(bridge.getServices()).hasSize(0); }
@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); }
@Test public void testWithAContainerWithTwoPorts() throws InterruptedException { CreateContainerResponse container = client.createContainerCmd("nginx") .withExposedPorts(ExposedPort.tcp(80), ExposedPort.tcp(443)) .withPortBindings(PortBinding.parse("80"), PortBinding.parse("443")) .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(2); client.stopContainerCmd(container.getId()).exec(); done.set(false); Future<Void> future3 = Future.future(); future3.setHandler(ar -> done.set(ar.succeeded())); bridge.scan(future3); await().untilAtomic(done, is(true)); assertThat(bridge.getServices()).hasSize(0); }