How to use removeImage method of org.testcontainers.utility.ResourceReaper class

Best Testcontainers-java code snippet using org.testcontainers.utility.ResourceReaper.removeImage

Source:ResourceReaper.java Github

copy

Full Screen

...203 */204 public synchronized void performCleanup() {205 registeredContainers.forEach(this::stopContainer);206 registeredNetworks.forEach(this::removeNetwork);207 registeredImages.forEach(this::removeImage);208 }209 /**210 * Register a filter to be cleaned up.211 *212 * @param filter the filter213 */214 public void registerFilterForCleanup(List<Map.Entry<String, String>> filter) {215 synchronized (DEATH_NOTE) {216 DEATH_NOTE.add(filter);217 DEATH_NOTE.notifyAll();218 }219 }220 /**221 * Register a container to be cleaned up, either on explicit call to stopAndRemoveContainer, or at JVM shutdown.222 *223 * @param containerId the ID of the container224 * @param imageName the image name of the container (used for logging)225 */226 public void registerContainerForCleanup(String containerId, String imageName) {227 setHook();228 registeredContainers.put(containerId, imageName);229 }230 /**231 * Stop a potentially running container and remove it, including associated volumes.232 *233 * @param containerId the ID of the container234 */235 public void stopAndRemoveContainer(String containerId) {236 stopContainer(containerId, registeredContainers.get(containerId));237 registeredContainers.remove(containerId);238 }239 /**240 * Stop a potentially running container and remove it, including associated volumes.241 *242 * @param containerId the ID of the container243 * @param imageName the image name of the container (used for logging)244 */245 public void stopAndRemoveContainer(String containerId, String imageName) {246 stopContainer(containerId, imageName);247 registeredContainers.remove(containerId);248 }249 private void stopContainer(String containerId, String imageName) {250 boolean running;251 try {252 InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(containerId).exec();253 running = containerInfo.getState() != null && Boolean.TRUE.equals(containerInfo.getState().getRunning());254 } catch (NotFoundException e) {255 LOGGER.trace("Was going to stop container but it apparently no longer exists: {}", containerId);256 return;257 } catch (Exception e) {258 LOGGER.trace("Error encountered when checking container for shutdown (ID: {}) - it may not have been stopped, or may already be stopped. Root cause: {}",259 containerId,260 Throwables.getRootCause(e).getMessage());261 return;262 }263 if (running) {264 try {265 LOGGER.trace("Stopping container: {}", containerId);266 dockerClient.killContainerCmd(containerId).exec();267 LOGGER.trace("Stopped container: {}", imageName);268 } catch (Exception e) {269 LOGGER.trace("Error encountered shutting down container (ID: {}) - it may not have been stopped, or may already be stopped. Root cause: {}",270 containerId,271 Throwables.getRootCause(e).getMessage());272 }273 }274 try {275 dockerClient.inspectContainerCmd(containerId).exec();276 } catch (Exception e) {277 LOGGER.trace("Was going to remove container but it apparently no longer exists: {}", containerId);278 return;279 }280 try {281 LOGGER.trace("Removing container: {}", containerId);282 dockerClient.removeContainerCmd(containerId).withRemoveVolumes(true).withForce(true).exec();283 LOGGER.debug("Removed container and associated volume(s): {}", imageName);284 } catch (Exception e) {285 LOGGER.trace("Error encountered shutting down container (ID: {}) - it may not have been stopped, or may already be stopped. Root cause: {}",286 containerId,287 Throwables.getRootCause(e).getMessage());288 }289 }290 /**291 * Register a network to be cleaned up at JVM shutdown.292 *293 * @param id the ID of the network294 */295 public void registerNetworkIdForCleanup(String id) {296 setHook();297 registeredNetworks.add(id);298 }299 /**300 * @param networkName the name of the network301 * @deprecated see {@link ResourceReaper#registerNetworkIdForCleanup(String)}302 */303 @Deprecated304 public void registerNetworkForCleanup(String networkName) {305 try {306 // Try to find the network by name, so that we can register its ID for later deletion307 dockerClient.listNetworksCmd()308 .withNameFilter(networkName)309 .exec()310 .forEach(network -> registerNetworkIdForCleanup(network.getId()));311 } catch (Exception e) {312 LOGGER.trace("Error encountered when looking up network (name: {})", networkName);313 }314 }315 /**316 * Removes a network by ID.317 * @param id318 */319 public void removeNetworkById(String id) {320 removeNetwork(id);321 }322 /**323 * Removes a network by ID.324 * @param identifier325 * @deprecated see {@link ResourceReaper#removeNetworkById(String)}326 */327 @Deprecated328 public void removeNetworks(String identifier) {329 removeNetworkById(identifier);330 }331 private void removeNetwork(String id) {332 try {333 List<Network> networks;334 try {335 // Try to find the network if it still exists336 // Listing by ID first prevents docker-java logging an error if we just go blindly into removeNetworkCmd337 networks = dockerClient.listNetworksCmd().withIdFilter(id).exec();338 } catch (Exception e) {339 LOGGER.trace("Error encountered when looking up network for removal (name: {}) - it may not have been removed", id);340 return;341 }342 // at this point networks should contain either 0 or 1 entries, depending on whether the network exists343 // using a for loop we essentially treat the network like an optional, only applying the removal if it exists344 for (Network network : networks) {345 try {346 dockerClient.removeNetworkCmd(network.getId()).exec();347 registeredNetworks.remove(network.getId());348 LOGGER.debug("Removed network: {}", id);349 } catch (Exception e) {350 LOGGER.trace("Error encountered removing network (name: {}) - it may not have been removed", network.getName());351 }352 }353 } finally {354 registeredNetworks.remove(id);355 }356 }357 public void unregisterNetwork(String identifier) {358 registeredNetworks.remove(identifier);359 }360 public void unregisterContainer(String identifier) {361 registeredContainers.remove(identifier);362 }363 public void registerImageForCleanup(String dockerImageName) {364 setHook();365 registeredImages.add(dockerImageName);366 }367 private void removeImage(String dockerImageName) {368 LOGGER.trace("Removing image tagged {}", dockerImageName);369 try {370 dockerClient.removeImageCmd(dockerImageName).withForce(true).exec();371 } catch (Throwable e) {372 LOGGER.warn("Unable to delete image " + dockerImageName, e);373 }374 }375 private void setHook() {376 if (hookIsSet.compareAndSet(false, true)) {377 // If the JVM stops without containers being stopped, try and stop the container.378 Runtime.getRuntime().addShutdownHook(new Thread(DockerClientFactory.TESTCONTAINERS_THREAD_GROUP, this::performCleanup));379 }380 }381 static class FilterRegistry {382 @VisibleForTesting383 static final String ACKNOWLEDGMENT = "ACK";384 private final BufferedReader in;...

Full Screen

Full Screen

removeImage

Using AI Code Generation

copy

Full Screen

1ResourceReaper.removeImage("testcontainers/ryuk:0.2.3")2ResourceReaper.removeImage("testcontainers/ryuk:0.2.2")3ResourceReaper.removeImage("testcontainers/ryuk:0.2.1")4ResourceReaper.removeImage("testcontainers/ryuk:0.2.0")5ResourceReaper.removeImage("testcontainers/ryuk:0.1.0")6ResourceReaper.removeImage("testcontainers/ryuk:0.0.1")7ResourceReaper.removeImage("testcontainers/ryuk:0.0.0")8ResourceReaper.removeImage("testcontainers/ryuk:0.1.1")9ResourceReaper.removeImage("testcontainers/ryuk:0.1.2")10ResourceReaper.removeImage("testcontainers/ryuk:0.1.3")11ResourceReaper.removeImage("testcontainers/ryuk:0.1.4")12ResourceReaper.removeImage("testcontainers/ryuk:0.1.5")13ResourceReaper.removeImage("testcontainers/ryuk:0.1.6")14ResourceReaper.removeImage("testcontainers/ryuk:0.1.7")15ResourceReaper.removeImage("testcontainers/ryuk:0.1.8")16ResourceReaper.removeImage("testcontainers/ryuk:0.1.9")17ResourceReaper.removeImage("testcontainers/ryuk:0.1.10")18ResourceReaper.removeImage("testcontainers/ryuk:0.1.11")19ResourceReaper.removeImage("testcontainers/ryuk:0.1.12")20ResourceReaper.removeImage("testcontainers/ryuk:0.1.13")21ResourceReaper.removeImage("testcontainers/ryuk:0.1.14")22ResourceReaper.removeImage("testcontainers/ryuk:0.1.15")23ResourceReaper.removeImage("testcontainers/ryuk:0.1.16")24ResourceReaper.removeImage("testcontainers/ryuk:0.1.17")25ResourceReaper.removeImage("testcontainers/ryuk:0.1.18")26ResourceReaper.removeImage("testcontainers/ryuk:0.1.19")27ResourceReaper.removeImage("testcontainers/ryuk:0.1.20")28ResourceReaper.removeImage("testcontainers/ryuk:

Full Screen

Full Screen

removeImage

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.utility;2import java.io.IOException;3import org.testcontainers.DockerClientFactory;4import org.testcontainers.utility.ResourceReaper;5public class ResourceReaperTest {6 public static void main(String[] args) throws IOException {7 ResourceReaper resourceReaper = new ResourceReaper();8 resourceReaper.removeImage("testcontainers/ryuk:0.3.0");9 }10}

Full Screen

Full Screen

removeImage

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.utility.ResourceReaper;2import org.testcontainers.utility.MountableFile;3import java.io.IOException;4{5 public static void main(String[] args) throws IOException, InterruptedException6 {7 ResourceReaper.instance().removeImage("mysql:5.7.21");8 ResourceReaper.instance().removeImage("mysql:5.7.21", true);9 }10}11import org.testcontainers.utility.ResourceReaper;12import org.testcontainers.utility.MountableFile;13import java.io.IOException;14{15 public static void main(String[] args) throws IOException, InterruptedException16 {17 ResourceReaper.instance().removeImage("mysql:5.7.21")

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