Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove existing image when using `docker commit`?

Tags:

docker

I have a script which I use to build docker images. The script starts up a docker container, executes some commands on it, and then does docker commit to fixturize the image. When I commit with an image name/tag, and then later commit with the same image name/tag, I would like the previous image to be removed, since at that point it's just taking up disk space. Instead it sticks around (I can see it in docker images, with its repository and tag both listed as <none>). Is there a way to have docker automatically remove these replaced images?

like image 934
limp_chimp Avatar asked Oct 18 '16 15:10

limp_chimp


People also ask

Can we delete a docker image when a running container is using it?

You cannot remove an image of a running container unless you use the -f option. To see all images on a host use the docker image ls command.

How do I remove an image from docker repository?

To remove the image, you first need to list all the images to get the Image IDs, Image name and other details. By running simple command docker images -a or docker images . After that you make sure which image want to remove, to do that executing this simple command docker rmi <your-image-id> .


1 Answers

Not automtaically, but Docker does know which layers are not used in images, and you can remove those "dangling" image layers like this:

docker rmi $(docker images -f "dangling=true" -q)

While you're clearing up, you can also remove exited containers:

docker rm $(docker ps -a -q)

And dangling volumes:

docker volume rm $(docker volume ls -qf dangling=true)
like image 138
Elton Stoneman Avatar answered Nov 16 '22 03:11

Elton Stoneman