private <T> T runInsideDocker(DockerClient client, Consumer<CreateContainerCmd> createContainerCmdConsumer, BiFunction<DockerClient, String, T> block) { checkAndPullImage(client, TINY_IMAGE); CreateContainerCmd createContainerCmd = client.createContainerCmd(TINY_IMAGE) .withLabels(DEFAULT_LABELS); createContainerCmdConsumer.accept(createContainerCmd); String id = createContainerCmd.exec().getId(); try { client.startContainerCmd(id).exec(); return block.apply(client, id); } finally { try { client.removeContainerCmd(id).withRemoveVolumes(true).withForce(true).exec(); } catch (NotFoundException | InternalServerErrorException ignored) { log.debug("", ignored); } } }
createCommand.withLabels(combinedLabels);
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); } }); } }
List<Bind> binds = new ArrayList<>(); binds.add(new Bind("//var/run/docker.sock", new Volume("/var/run/docker.sock"))); String ryukContainerId = client.createContainerCmd(ryukImage).withHostConfig(new HostConfig().withAutoRemove(true)).withExposedPorts(new ExposedPort(8080)).withPublishAllPorts(true).withName("testcontainers-ryuk-" + DockerClientFactory.SESSION_ID).withLabels(Collections.singletonMap(DockerClientFactory.TESTCONTAINERS_LABEL, "true")).withBinds(binds).withPrivileged(TestcontainersConfiguration.getInstance().isRyukPrivileged()).exec().getId(); client.startContainerCmd(ryukContainerId).exec(); InspectContainerResponse inspectedContainer = client.inspectContainerCmd(ryukContainerId).exec();
.withLabels(labels) .withEnv(environmentAssignments) .withBinds(volumeBinds)
public void startNode(String id, BrowserType browserType, String nodeName, String imageId, boolean record, String containerIp) { // Create node pullImageIfNecessary(imageId, true); log.debug("Creating container for browser '{}'", id); CreateContainerCmd createContainerCmd = getClient().createContainerCmd(imageId).withPrivileged(true).withCapAdd(SYS_ADMIN).withName(nodeName); mountDefaultFolders(createContainerCmd); mountFiles(createContainerCmd); createContainerCmd.withNetworkMode("none"); Map<String, String> labels = new HashMap<>(); labels.put("KurentoDnat", "true"); labels.put("Transport", getProperty(TEST_SELENIUM_TRANSPORT)); labels.put("IpAddress", containerIp); createContainerCmd.withLabels(labels); createContainerCmd.exec(); log.debug("Container {} started...", nodeName); // Start node if stopped startContainer(nodeName); startRecordingIfNeeded(id, nodeName, record); logMounts(nodeName); logNetworks(nodeName); }
public void startNode(String id, BrowserType browserType, String nodeName, String imageId, boolean record, String containerIp) { // Create node pullImageIfNecessary(imageId, true); log.debug("Creating container for browser '{}'", id); CreateContainerCmd createContainerCmd = getClient().createContainerCmd(imageId).withPrivileged(true).withCapAdd(SYS_ADMIN).withName(nodeName); mountDefaultFolders(createContainerCmd); mountFiles(createContainerCmd); createContainerCmd.withNetworkMode("none"); Map<String, String> labels = new HashMap<>(); labels.put("KurentoDnat", "true"); labels.put("Transport", getProperty(TEST_SELENIUM_TRANSPORT)); labels.put("IpAddress", containerIp); createContainerCmd.withLabels(labels); createContainerCmd.exec(); log.debug("Container {} started...", nodeName); // Start node if stopped startContainer(nodeName); startRecordingIfNeeded(id, nodeName, record); logMounts(nodeName); logNetworks(nodeName); }
map.put(DockerContainerLabelKeys.CONTAINER_IMAGE, getImage()); containerConfig.withLabels(map);
CreateContainerCmd createContainerCmd = client.createContainerCmd(TINY_IMAGE).withLabels(DEFAULT_LABELS); createContainerCmdConsumer.accept(createContainerCmd); String id = createContainerCmd.exec().getId();
@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"); }
createContainerCmd.withLabels(labels);
createContainerCmd.withLabels(labels);
@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"); }
protected void createContainer(DockerClient dockerClient) throws InterruptedException { pullImage(dockerClient); CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd(properties.getDockerImage()) // NOSONAR .withLabels(getAllLabels()) .withEnv(getEnvs()) .withHostConfig(buildHostConfig()); adjustCreateCommand(createContainerCmd); String containerId = createContainerCmd.exec().getId(); dockerClient.startContainerCmd(containerId).exec(); containerInfo = dockerClient.inspectContainerCmd(containerId).exec(); }
createContainerCmd.withLabels(containerConfiguration.getLabels());
createContainerCmd.withLabels(containerConfiguration.getLabels());