bmuschko/gradle-docker-plugin

Remove multiple images at once

red55 opened this issue · 3 comments

red55 commented

Hi,
First of all, thanks for outstanding plugin, guys.

It would be great if I could delete multiple docker images with a single Gradle task.

Expected Behavior

Additional task DockerRemoveImages for example or upgrade current DockerRemoveImage to accept a list of image Ids. No need for atomic remove for me at least.

Current Behavior

There is no way to delete multiple images at once or I couldn't find it. Anyone please guide me if I'm missing something. Now I have to do something like that:

def imagesToClean = []

import com.bmuschko.gradle.docker.tasks.image.*
import com.github.dockerjava.api.command.RemoveImageCmd;
import com.bmuschko.gradle.docker.tasks.AbstractDockerRemoteApiTask;
abstract class DockerRemoveImages extends AbstractDockerRemoteApiTask {

    @Input
    public final SetProperty<String> getImageIds() {
        return imageIds
    }
    private final SetProperty<String> imageIds = project.objects.setProperty(String.class);

    @Override
    public void runRemoteCommand() {
        getImageIds().get().each {
            i -> 
                getLogger().quiet("Removing image with ID {}.", i);
                                
                RemoveImageCmd removeImageCmd = getDockerClient().removeImageCmd(i);
                removeImageCmd.withForce(Boolean.TRUE);

                removeImageCmd.exec();                
        }
    }
}

tasks.register('listLocalImages', DockerListImages) {    
    imageName = DOCKER_IMAGE_NAME
    onNext { i -> imagesToClean.add(i.Id) }
}

tasks.register('cleanLocalImages', DockerRemoveImages) {
    dependsOn tasks.named('listLocalImages')
    
    imageIds = imagesToClean    
}

Sure, its a workaround. But...

Context

On my build server there are a lot of Docker images left which were already pushed to remote registry. Would like to have an easy way to remove them as a part of Gradle script. Also, if Gradle script fails after image has been built its a good idea on next successful run remove any stale images. Sure, there could be more than just one.

Your Environment

plugins {
    id 'com.bmuschko.docker-remote-api' version '9.3.2'
}
./gradlew --version

------------------------------------------------------------
Gradle 8.3
------------------------------------------------------------

Build time:   2023-08-17 07:06:47 UTC
Revision:     8afbf24b469158b714b36e84c6f4d4976c86fcd5

Kotlin:       1.9.0
Groovy:       3.0.17
Ant:          Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM:          19.0.2 (Private Build 19.0.2+7-Ubuntu-0ubuntu322.04)
OS:           Linux 6.2.0-26-generic amd64

Given that some other tasks already use a list of images, I think we should do the same here. A way forward would be to deprecate the existing field and introduce a new field as replacement. Is this something you are interested in working on in the form of a pull request?

red55 commented

Hi @bmuschko sure, good idea to enhance RemoveImage with a list of images. Unfrotunatly I'm not sure I have enough time to make a PR, but i'll do my best in the next few weeks.

red55 commented

Hi @bmuschko,
How do you think, should i add new task like DockerRemove[Multiple]Images or shoud I extend existing DockerRemoveImage to support array of image ids?