limp_chimp
limp_chimp

Reputation: 15233

Remove existing image when using `docker commit`?

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?

Upvotes: 9

Views: 6103

Answers (1)

Elton Stoneman
Elton Stoneman

Reputation: 19194

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)

Upvotes: 8

Related Questions