From 65688954c9f4a0393d58c695c12712707f47c527 Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Fri, 6 May 2022 15:25:14 +0200 Subject: Improve container removal logic Signed-off-by: Georg Pfuetzenreuter --- .../java/net/libertacasa/pubsh/web/Docker.java | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/net/libertacasa/pubsh/web/Docker.java b/src/main/java/net/libertacasa/pubsh/web/Docker.java index 637f22d..536a80e 100644 --- a/src/main/java/net/libertacasa/pubsh/web/Docker.java +++ b/src/main/java/net/libertacasa/pubsh/web/Docker.java @@ -1,9 +1,13 @@ package net.libertacasa.pubsh.web; import java.time.Duration; +import java.util.Arrays; +import java.util.Collection; import java.util.List; import java.util.stream.Collectors; import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.command.InspectExecResponse.Container; +import com.github.dockerjava.api.command.ListContainersCmd; import com.github.dockerjava.api.model.Image; import com.github.dockerjava.core.DefaultDockerClientConfig; import com.github.dockerjava.core.DockerClientConfig; @@ -49,6 +53,27 @@ public class Docker { } public static void deleteContainer(String id) { + Collection stringifiedId = Arrays.asList(id); + System.out.println(id); + System.out.println(stringifiedId); + List containerQuery = dockerClient.listContainersCmd().withShowAll(false).withIdFilter(stringifiedId).exec(); + //apparently listContainersCmd returns a bunch of garbled crap, hence the less ideal toString/contains + if(! containerQuery.toString().contains(id)) { + System.out.println("Not found in running containers ..."); + List containerQueryAll = dockerClient.listContainersCmd().withShowAll(true).withIdFilter(stringifiedId).exec(); + if(containerQueryAll.toString().contains(id)) { + System.out.println("Found in stopped containers ..."); + } + if(! containerQueryAll.toString().contains(id)) { + System.out.println("Not found at all. The fuck."); + } + } + if(containerQuery.toString().contains(id)) { + System.out.println("Found in running containers, stopping ..."); + dockerClient.stopContainerCmd(id).exec(); + } + + //System.out.println(containerQuery); dockerClient.removeContainerCmd(id).exec(); } -- cgit v1.2.3