How can I delete containers by using docker?

138    Asked by ClaudineTippins in Devops , Asked on Jul 1, 2024

I am currently engaged in a particular task that is related to managing a docker-based environment and suddenly I have noticed that my server is running out of disk space. When I investigated, I found that many stopped containers are consuming space. How can I use docker to delete these stopped containers so that I can free up the space in my server? 

Answered by David

 In the context of DevOps, if you want to delete the stopped containers in the docker and want to free up disk space, then you can use the following command:

“docker container prune”

Explanation

Identifying stopped containers

Firstly, you would need to list all the containers and then identify the ones that are stopped by using :

  “docker ps- a”

Removing stopped containers

Now you can remove all the stopped containers by using

  “docker container prune”

Force removal

If you want to avoid the confirmation prompt so that you can forcibly delete then you can use the -f flag

  “docker container prune -f”

Here is a java based approach given:-


Creating java program

Import com.github.dockerjava.api.DockerClient;
Import com.github.dockerjava.api.model.Container;
Import com.github.dockerjava.core.DockerClientBuilder;
Import com.github.dockerjava.core.DockerClientConfig;
Import com.github.dockerjava.core.DefaultDockerClientConfig;
Import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
Import com.github.dockerjava.transport.DockerHttpClient;
Import java.util.List;
Public class DockerContainerManager {
    Public static void main(String[] args) {
        // Docker daemon connection configuration
        DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withDockerHost(“unix:///var/run/docker.sock”) // Adjust if your Docker daemon runs on a different host
                .build();
        DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
                .dockerHost(config.getDockerHost())
                .build();
        DockerClient dockerClient = DockerClientBuilder.getInstance(config)
                .withDockerHttpClient(httpClient)
                .build();
        Try {
            // List all containers including stopped ones
            List containers = dockerClient.listContainersCmd().withShowAll(true).exec();
            For (Container container : containers) {
                System.out.println(“Container ID: “ + container.getId());
                System.out.println(“Status: “ + container.getStatus());
                // Check if the container is stopped
                If (container.getStatus().startsWith(“Exited”)) {
                    System.out.println(“Container “ + container.getId() + “ is stopped.”);
                } else {
                    System.out.println(“Container “ + container.getId() + “ is running.”);
                }
            }
        } finally {
            // Close the Docker client
            dockerClient.close();
        }
    }
}
Listing all the containers
Import com.github.dockerjava.api.DockerClient;
Import com.github.dockerjava.api.model.Container;
Import com.github.dockerjava.core.DockerClientBuilder;
Import com.github.dockerjava.core.DockerClientConfig;
Import com.github.dockerjava.core.DefaultDockerClientConfig;
Import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
Import com.github.dockerjava.transport.DockerHttpClient;
Import java.util.List;
Public class DockerContainerManager {
    Public static void main(String[] args) {
        // Docker daemon connection configuration
        DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withDockerHost(“unix:///var/run/docker.sock”) // Adjust if your Docker daemon runs on a different host
                .build();
        DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
                .dockerHost(config.getDockerHost())
                .build();
        DockerClient dockerClient = DockerClientBuilder.getInstance(config)
                .withDockerHttpClient(httpClient)
                .build();
        Try {
            // List all containers including stopped ones
            List containers = dockerClient.listContainersCmd().withShowAll(true).exec();
            For (Container container : containers) {
                System.out.println(“Container ID: “ + container.getId());
                System.out.println(“Status: “ + container.getStatus());
                // Check if the container is stopped
                If (container.getStatus().startsWith(“Exited”)) {
                    System.out.println(“Container “ + container.getId() + “ is stopped.”);
                } else {
                    System.out.println(“Container “ + container.getId() + “ is running.”);
                }
            }
        } finally {
            // Close the Docker client
            dockerClient.close();
        }
    }
}
Checking status and remove stopped containers
Import com.github.dockerjava.api.DockerClient;
Import com.github.dockerjava.api.model.Container;
Import com.github.dockerjava.core.DockerClientBuilder;
Import com.github.dockerjava.core.DockerClientConfig;
Import com.github.dockerjava.core.DefaultDockerClientConfig;
Import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
Import com.github.dockerjava.transport.DockerHttpClient;
Import java.util.List;
Public class DockerContainerManager {
    Public static void main(String[] args) {
        // Docker daemon connection configuration
        DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withDockerHost(“unix:///var/run/docker.sock”) // Adjust if your Docker daemon runs on a different host
                .build();
        DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
                .dockerHost(config.getDockerHost())
                .build();
        DockerClient dockerClient = DockerClientBuilder.getInstance(config)
                .withDockerHttpClient(httpClient)
                .build();
        Try {
            // Example: List all containers
            listContainers(dockerClient);
            // Example: Check container status and perform action
            checkAndActOnContainers(dockerClient);
        } finally {
            // Close the Docker client
            dockerClient.close();
        }
    }
    // Example method: List all containers
    Private static void listContainers(DockerClient dockerClient) {
        List containers = dockerClient.listContainersCmd().exec();
        For (Container container : containers) {
            System.out.println(“Container ID: “ + container.getId());
            System.out.println(“Image: “ + container.getImage());
            System.out.println(“Status: “ + container.getStatus());
            System.out.println(“---------------------“);
        }
    }

    // Example method: Check container status and perform action

    Private static void checkAndActOnContainers(DockerClient dockerClient) {
        List containers = dockerClient.listContainersCmd().exec();
        For (Container container : containers) {
            String containerId = container.getId();
            String status = container.getStatus();
            If (status.startsWith(“Exited”)) {
                // Container is stopped, perform action (e.g., remove container)
                System.out.println(“Container “ + containerId + “ is stopped. Removing…”);
                // Remove container
                dockerClient.removeContainerCmd(containerId).exec();
                System.out.println(“Container “ + containerId + “ removed.”);
            } else if (status.startsWith(“Up”)) {
                // Container is running, perform other actions as needed
                System.out.println(“Container “ + containerId + “ is running.”);
                // Add other actions here based on container status
            } else {


Your Answer

Interviews

Parent Categories