docker-maven-plugin icon indicating copy to clipboard operation
docker-maven-plugin copied to clipboard

Remove old image if already exists when doing a docker:build

Open benathmane opened this issue 9 years ago • 18 comments

Hello,

what's the argument to remove or not creat container ? My command line : mvn clean package install docker:build Thanks

benathmane avatar Jun 29 '16 12:06 benathmane

Sorry, didn't get the question. What is your issue ?

rhuss avatar Jun 29 '16 12:06 rhuss

I want to know if there's an argument (not a command line) to remove containers after a mvn clean package install docker:build :)

benathmane avatar Jun 29 '16 12:06 benathmane

docker:build doesnt create containers, only images.

docker:start creates containers and they can be removed at docker:stop after running with the option keepContainer=false. There is also a docker:remove goal for removing images.

Please let me know when and where the manual is not clear enough so that I can fix that.

rhuss avatar Jun 29 '16 12:06 rhuss

Sorry, after this command : "mvn clean package install docker:build"

I have :

hygieia-api                           latest              cb5a97d238b5        4 minutes ago       708 MB
<none>                                <none>              c117c90f967d        47 minutes ago      700.8 MB
<none>                                <none>              b7ba55168775        47 minutes ago      700.8 MB
<none>                                <none>              d98bc12b591c        47 minutes ago      700.1 MB
<none>                                <none>              fc7b12aa101b        47 minutes ago      699.3 MB
<none>                                <none>              3834b5446f96        47 minutes ago      708 MB

I do not understand why it creates images :(

benathmane avatar Jun 29 '16 12:06 benathmane

These are different layers which are stacked (and the size is sum of this layer + all layers below). That is a normal Docker feature (layered filesystem), you can remove any of the layer without destroying the whole image.

rhuss avatar Jun 29 '16 12:06 rhuss

Yes with this command : docker images|sed "1 d"|grep |awk '{print $3}'|sort|uniq|xargs docker rmi

thanks :)

benathmane avatar Jun 29 '16 12:06 benathmane

Ok, so you mean to clean up images from a previous build, right ?

BTW, the easiest way to remove dangling images is docker rmi $(docker images -qa -f 'dangling=true')

rhuss avatar Jun 29 '16 12:06 rhuss

Thanks :+1:

benathmane avatar Jun 29 '16 13:06 benathmane

Yeah, might make sense that if an image with the given name already exists when building that it could be (optionally) deleted.

rhuss avatar Jun 29 '16 13:06 rhuss

the plugin already cleans up images from a previous build provided that image is still properly tagged/labeled.

jgangemi avatar Jul 06 '16 17:07 jgangemi

I face the same problem after building and pushing image with same name and tag previous one is not getting deleted, wonder if there is a way for plugin to do it automatically. Otherwise the command provided by rhuss is the option docker rmi $(docker images -qa -f 'dangling=true')

mvn package dockerfile:build dockerfile:push

as you can see the new image was created with tag latest while old was left in local registry

REPOSITORY                                  TAG                 IMAGE ID            CREATED             SIZE
servername.azurecr.io/gs-spring-boot-docker   latest              2a356c654697        23 seconds ago      119MB
servername.azurecr.io/gs-spring-boot-docker   <none>              f8d0d3b1dd60        39 minutes ago      119MB
openjdk                                     8-jdk-alpine        224765a6bdbe        2 months ago        102MB

rhollins avatar Mar 31 '18 17:03 rhollins

docker rmi $(docker images -qa -f 'dangling=true')

wtf? Is this really the best way to not fill up a .qcow with old versions of a build you don't care about? Can't you just overwrite the old one or something? Wouldn't this kind of feature fall under "basic functionality" rather than something you have to do by hand?

SephReed avatar Aug 15 '19 21:08 SephReed

Execute "docker image prune" to delete old versions See details on: https://stackoverflow.com/questions/45142528/what-is-a-dangling-image-and-what-is-an-unused-image

ulrichmeyer avatar Sep 19 '20 16:09 ulrichmeyer

I can easily

docker image rm $(DOCKER_IMAGE)
docker build -t $(DOCKER_IMAGE) .

in a bash or make script but if the image doesn't exist then everything comes crashing down. Is there no option to delete an image if it exists?

EDIT
Sorry, just realised that I could do this instead

docker image rm $(DOCKER_IMAGE) || (echo "Image $(DOCKER_IMAGE) didn't exist so not removed."; exit 0)
docker build -t $(DOCKER_IMAGE) .

grofte avatar Nov 19 '21 11:11 grofte

thanks @grofte, good idea.

However, if there's a container that runs and has been created from the same image then docker won't allow you to delete the image.

szabikr avatar Nov 20 '21 17:11 szabikr

thanks @grofte, good idea.

However, if there's a container that runs and has been created from the same image then docker won't allow you to delete the image.

I usually use docker run --rm=true which closes down the container after use. I don't exactly know why it isn't default. You could also force the deletion but I don't know if I want to automate that. There might be a reason why the container is still running.

EDIT: This should also work for removing an image without an error if it doesn't exist - again you can force it if you have containers based on the image running

docker image rm $(docker images | tr -s ' ' : | grep $DOCKER_IMAGE | cut -f3 -d:)

grofte avatar Nov 22 '21 13:11 grofte

I'm running

docker rmi myimage
docker build -t myimage .

in a batch (Docker Desktop on Windows) and I also feel like this should be doable in a single command, ignoring the initial remove if the image doesn't exist.

J3ronimo avatar Apr 28 '22 12:04 J3ronimo

docker system prune

dpresbit avatar Sep 18 '23 22:09 dpresbit