user3855077
user3855077

Reputation: 3

Update docker image resulting in orphan image

I can use

docker build -t yzx2003209/my_image .

to build a new image tagged by yzx2003209/my_image:latest

However, the original yzx2003209/my_image:latest will become a <none> image and I have to manually rmi this orphan image.

Is there any way I can just update a image without manually removing orphan images?

Upvotes: 0

Views: 1057

Answers (1)

ISanych
ISanych

Reputation: 22710

There is no way to update an image - images are immutable - when you execute docker build which produce different image it also moving tag there - nothing changed in old image itself - it just don't have a tag anymore. If you want to remove all such images you could execute next command after docker build:

docker build -t yzx2003209/my_image .
docker rmi `docker images -q --filter "dangling=true"`

This command will delete ALL images without tag. Or you could do it in a different order - remove your old image before docker build - while it is still have a tag:

docker rmi yzx2003209/my_image
docker build -t yzx2003209/my_image .

But if your build will fail you will have no image. You could get image id before build and remove image after successful build:

old=`docker images -q yzx2003209/my_image`
docker build -t yzx2003209/my_image .
docker rmi $old

You could also use --rm option to remove intermediate containers after a successful build:

docker build --rm -t yzx2003209/my_image .
docker rmi `docker images -q --filter "dangling=true"`

Upvotes: 2

Related Questions