How to troubleshoot and resolve low disk space on a server?

246    Asked by ColinPayne in Devops , Asked on May 30, 2024

 I am currently engaged in a particular task that is related to managing a docker environment for a team of developers. One day I noticed that the disk space of the server was running critically low. How can I use the “docker prune images” command to alleviate this issue without affecting the running containers or essential images? 

Answered by Connor Peake

 In the context of DevOps, you can address the low disk space issue in your particular docker environment even without affecting the running containers or even the essential images by using the “docker prune images” command along with some specific potions. Here is how you can do so:-

Firstly, you would need to open your terminal or command prompt.

Now you should try to run the following command to clean up unused docker images while keeping the images that are necessary by running containers or even specified by their digests:-

You can adjust the “until” value according to how you far back to preserve images and ensure that the “label” should match with the labels which you have applied to the images which you want to be kept.

After running this above Command the docker will remove only the unused images that would be older than the specified time which would ensure that your essential images and running containers should be remain unaffected.

You can achieve the same functionality by using the docker Remote API in Java programming language. Here is an example coding given by using the docker API library:-

Import com.github.dockerjava.api.DockerClient;
Import com.github.dockerjava.api.command.PruneImagesCmd;
Import com.github.dockerjava.api.model.PruneImagesResponse;
Import com.github.dockerjava.core.DockerClientBuilder;
Import com.github.dockerjava.core.command.PruneImagesCmdImpl;
Import java.util.List;
Public class DockerImagePruner {
    Public static void main(String[] args) {
        Try (DockerClient dockerClient = DockerClientBuilder.getInstance().build()) {
            PruneImagesCmd pruneImagesCmd = new PruneImagesCmdImpl(dockerClient);
            pruneImagesCmd.withFilters(“until=24h”, “label!=keep”);
            PruneImagesResponse response = pruneImagesCmd.exec();
            List deletedImages = response.getImagesDeleted();
            System.out.println(“Deleted images:”);
            For (String image : deletedImages) {
                System.out.println(image);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Here is also an example given by using the Python programming language which would cover error handling and include commenting for the each step. Here is an expanded version of the docker image pruning script given by using the Python programming language:-

Import docker
From docker.errors import APIError
Def prune_docker_images():
    Try:
        # Connect to the Docker daemon
        Client = docker.from_env()
        # Define filters to prune images older than 24 hours and exclude specific labels
        Filters = {
            ‘until’: ‘24h’, # Images not created in the last 24 hours
            ‘label’: [‘keep=false’] # Exclude images labeled with ‘keep=false’
        }
        # Perform image pruning operation with the specified filters
        Pruned_images = client.images.prune(filters=filters)
        # Print the names of deleted images
        Print(“Deleted images:”)
        For image in pruned_images[‘ImagesDeleted’]:
            Print(image[‘Deleted’])
    Except APIError as e:
        Print(f”Error: {e}”)
    Except Exception as e:
        Print(f”An unexpected error occurred: {e}”)
If __name__ == “__main__”:
    Prune_docker_images()


Your Answer

Interviews

Parent Categories