How to use dockerRemoveImagesType method of org.testcontainers.containers.DockerComposeContainer class

Best Testcontainers-java code snippet using org.testcontainers.containers.DockerComposeContainer.dockerRemoveImagesType

Source:DockerComposeContainer.java Github

copy

Full Screen

...272 ambassadorContainer.stop();273 // Kill the services using docker-compose274 String cmd = "down -v";275 if (removeImages != null) {276 cmd += " --rmi " + removeImages.dockerRemoveImagesType();277 }278 runWithCompose(cmd);279 } finally {280 project = randomProjectId();281 }282 }283 }284 public SELF withExposedService(String serviceName, int servicePort) {285 return withExposedService(serviceName, servicePort, Wait.defaultWaitStrategy());286 }287 public DockerComposeContainer withExposedService(String serviceName, int instance, int servicePort) {288 return withExposedService(serviceName + "_" + instance, servicePort);289 }290 public DockerComposeContainer withExposedService(String serviceName, int instance, int servicePort, WaitStrategy waitStrategy) {291 return withExposedService(serviceName + "_" + instance, servicePort, waitStrategy);292 }293 public SELF withExposedService(String serviceName, int servicePort, @NonNull WaitStrategy waitStrategy) {294 String serviceInstanceName = getServiceInstanceName(serviceName);295 /*296 * For every service/port pair that needs to be exposed, we register a target on an 'ambassador container'.297 *298 * The ambassador container's role is to link (within the Docker network) to one of the299 * compose services, and proxy TCP network I/O out to a port that the ambassador container300 * exposes.301 *302 * This avoids the need for the docker compose file to explicitly expose ports on all the303 * services.304 *305 * {@link GenericContainer} should ensure that the ambassador container is on the same network306 * as the rest of the compose environment.307 */308 // Ambassador container will be started together after docker compose has started309 int ambassadorPort = nextAmbassadorPort.getAndIncrement();310 ambassadorPortMappings.computeIfAbsent(serviceInstanceName, __ -> new ConcurrentHashMap<>()).put(servicePort, ambassadorPort);311 ambassadorContainer.withTarget(ambassadorPort, serviceInstanceName, servicePort);312 ambassadorContainer.addLink(new FutureContainer(this.project + "_" + serviceInstanceName), serviceInstanceName);313 addWaitStrategy(serviceInstanceName, waitStrategy);314 return self();315 }316 private String getServiceInstanceName(String serviceName) {317 String serviceInstanceName = serviceName;318 if (!serviceInstanceName.matches(".*_[0-9]+")) {319 serviceInstanceName += "_1"; // implicit first instance of this service320 }321 return serviceInstanceName;322 }323 /*324 * can have multiple wait strategies for a single container, e.g. if waiting on several ports325 * if no wait strategy is defined, the WaitAllStrategy will return immediately.326 * The WaitAllStrategy uses an long timeout, because timeouts should be handled by the inner strategies.327 */328 private void addWaitStrategy(String serviceInstanceName, @NonNull WaitStrategy waitStrategy) {329 final WaitAllStrategy waitAllStrategy = waitStrategyMap.computeIfAbsent(serviceInstanceName, __ ->330 new WaitAllStrategy(WaitAllStrategy.Mode.WITH_MAXIMUM_OUTER_TIMEOUT).withStartupTimeout(Duration.ofMinutes(30)));331 waitAllStrategy.withStrategy(waitStrategy);332 }333 /**334 * Specify the {@link WaitStrategy} to use to determine if the container is ready.335 *336 * @param serviceName the name of the service to wait for337 * @param waitStrategy the WaitStrategy to use338 * @return this339 * @see org.testcontainers.containers.wait.strategy.Wait#defaultWaitStrategy()340 */341 public SELF waitingFor(String serviceName, @NonNull WaitStrategy waitStrategy) {342 String serviceInstanceName = getServiceInstanceName(serviceName);343 addWaitStrategy(serviceInstanceName, waitStrategy);344 return self();345 }346 /**347 * Get the host (e.g. IP address or hostname) that an exposed service can be found at, from the host machine348 * (i.e. should be the machine that's running this Java process).349 * <p>350 * The service must have been declared using DockerComposeContainer#withExposedService.351 *352 * @param serviceName the name of the service as set in the docker-compose.yml file.353 * @param servicePort the port exposed by the service container.354 * @return a host IP address or hostname that can be used for accessing the service container.355 */356 public String getServiceHost(String serviceName, Integer servicePort) {357 return ambassadorContainer.getHost();358 }359 /**360 * Get the port that an exposed service can be found at, from the host machine361 * (i.e. should be the machine that's running this Java process).362 * <p>363 * The service must have been declared using DockerComposeContainer#withExposedService.364 *365 * @param serviceName the name of the service as set in the docker-compose.yml file.366 * @param servicePort the port exposed by the service container.367 * @return a port that can be used for accessing the service container.368 */369 public Integer getServicePort(String serviceName, Integer servicePort) {370 Map<Integer, Integer> portMap = ambassadorPortMappings.get(getServiceInstanceName(serviceName));371 if (portMap == null) {372 throw new IllegalArgumentException("Could not get a port for '" + serviceName + "'. " +373 "Testcontainers does not have an exposed port configured for '" + serviceName + "'. " +374 "To fix, please ensure that the service '" + serviceName + "' has ports exposed using .withExposedService(...)");375 } else {376 return ambassadorContainer.getMappedPort(portMap.get(servicePort));377 }378 }379 public SELF withScaledService(String serviceBaseName, int numInstances) {380 scalingPreferences.put(serviceBaseName, numInstances);381 return self();382 }383 public SELF withEnv(String key, String value) {384 env.put(key, value);385 return self();386 }387 public SELF withEnv(Map<String, String> env) {388 env.forEach(this.env::put);389 return self();390 }391 /**392 * Use a local Docker Compose binary instead of a container.393 *394 * @return this instance, for chaining395 */396 public SELF withLocalCompose(boolean localCompose) {397 this.localCompose = localCompose;398 return self();399 }400 /**401 * Whether to pull images first.402 *403 * @return this instance, for chaining404 */405 public SELF withPull(boolean pull) {406 this.pull = pull;407 return self();408 }409 /**410 * Whether to tail child container logs.411 *412 * @return this instance, for chaining413 */414 public SELF withTailChildContainers(boolean tailChildContainers) {415 this.tailChildContainers = tailChildContainers;416 return self();417 }418 /**419 * Attach an output consumer at container startup, enabling stdout and stderr to be followed, waited on, etc.420 * <p>421 * More than one consumer may be registered.422 *423 * @param serviceName the name of the service as set in the docker-compose.yml file424 * @param consumer consumer that output frames should be sent to425 * @return this instance, for chaining426 */427 public SELF withLogConsumer(String serviceName, Consumer<OutputFrame> consumer) {428 String serviceInstanceName = getServiceInstanceName(serviceName);429 final List<Consumer<OutputFrame>> consumers = this.logConsumers.getOrDefault(serviceInstanceName, new ArrayList<>());430 consumers.add(consumer);431 this.logConsumers.putIfAbsent(serviceInstanceName, consumers);432 return self();433 }434 /**435 * Whether to always build images before starting containers.436 *437 * @return this instance, for chaining438 */439 public SELF withBuild(boolean build) {440 this.build = build;441 return self();442 }443 /**444 * Adds options to the docker-compose command, e.g. docker-compose --compatibility.445 *446 * @return this instance, for chaining447 */448 public SELF withOptions(String... options) {449 this.options = new HashSet<>(Arrays.asList(options));450 return self();451 }452 /**453 * Remove images after containers shutdown.454 *455 * @return this instance, for chaining456 */457 public SELF withRemoveImages(RemoveImages removeImages) {458 this.removeImages = removeImages;459 return self();460 }461 public Optional<ContainerState> getContainerByServiceName(String serviceName) {462 return Optional.ofNullable(serviceInstanceMap.get(serviceName));463 }464 private void followLogs(String containerId, Consumer<OutputFrame> consumer) {465 LogUtils.followOutput(DockerClientFactory.instance().client(), containerId, consumer);466 }467 private SELF self() {468 return (SELF) this;469 }470 private String randomProjectId() {471 return identifier + Base58.randomString(6).toLowerCase();472 }473 public enum RemoveImages {474 /**475 * Remove all images used by any service.476 */477 ALL("all"),478 /**479 * Remove only images that don't have a custom tag set by the `image` field.480 */481 LOCAL("local");482 private final String dockerRemoveImagesType;483 RemoveImages(final String dockerRemoveImagesType) {484 this.dockerRemoveImagesType = dockerRemoveImagesType;485 }486 public String dockerRemoveImagesType() {487 return dockerRemoveImagesType;488 }489 }490}491interface DockerCompose {492 String ENV_PROJECT_NAME = "COMPOSE_PROJECT_NAME";493 String ENV_COMPOSE_FILE = "COMPOSE_FILE";494 DockerCompose withCommand(String cmd);495 DockerCompose withEnv(Map<String, String> env);496 void invoke();497}498/**499 * Use Docker Compose container.500 */501class ContainerisedDockerCompose extends GenericContainer<ContainerisedDockerCompose> implements DockerCompose {...

Full Screen

Full Screen

dockerRemoveImagesType

Using AI Code Generation

copy

Full Screen

1 command: /bin/sh -c "while true; do echo hello world; sleep 1; done"2import org.testcontainers.containers.DockerComposeContainer3import org.testcontainers.containers.wait.strategy.Wait4def compose = new DockerComposeContainer(new File("docker-compose.yml"))5 .withExposedService("test", 80, Wait.forLogMessage(".*", 1))6 .start()7compose.stop()8import org.testcontainers.containers.DockerComposeContainer9import org.testcontainers.containers.wait.strategy.Wait10def compose = new DockerComposeContainer(new File("docker-compose.yml"))11 .withExposedService("test", 80, Wait.forLogMessage(".*", 1))12 .start()13compose.stop()14import org.testcontainers.containers.DockerComposeContainer15import org.testcontainers.containers.wait.strategy.Wait

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful