Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove multiple images at once #1190

Open
red55 opened this issue Aug 21, 2023 · 3 comments
Open

Remove multiple images at once #1190

red55 opened this issue Aug 21, 2023 · 3 comments

Comments

@red55
Copy link

red55 commented Aug 21, 2023

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
@bmuschko
Copy link
Owner

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
Copy link
Author

red55 commented Sep 3, 2023

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
Copy link
Author

red55 commented Oct 5, 2023

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?

red55 added a commit to red55/gradle-docker-plugin that referenced this issue Oct 5, 2023
red55 added a commit to red55/gradle-docker-plugin that referenced this issue Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants