Remove old image if already exists when doing a docker:build
Hello,
what's the argument to remove or not creat
Sorry, didn't get the question. What is your issue ?
I want to know if there's an argument (not a command line) to remove
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.
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
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.
Yes with this command : docker images|sed "1 d"|grep
thanks :)
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')
Thanks :+1:
Yeah, might make sense that if an image with the given name already exists when building that it could be (optionally) deleted.
the plugin already cleans up images from a previous build provided that image is still properly tagged/labeled.
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
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?
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
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) .
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.
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:)
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.
docker system prune