lolski
lolski

Reputation: 17501

If I build a new docker image with the same name as existing ones, will the old ones be overwritten?

If I build a new docker image with the same name as existing ones, will the old ones be overwritten?

Upvotes: 89

Views: 90864

Answers (6)

Kalpit
Kalpit

Reputation: 191

If the base image is the same, the existing image will be overwritten.

Else building a new docker image with the same name as the existing ones will NOT delete or overwrite the existing image. It will remove the same (name) tag from the existing image and create a new image with that name.

The old/existing image will have no tags. It will be shown as <none>.

You can rename the existing image before creating an image with the same name by using the following command:

docker tag oldNameTag newNameTag

You can delete the images with <none> tag using the following command. It will delete all the dangling images.

docker image prune

or

docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

More details can be found here.

Upvotes: 3

hpl002
hpl002

Reputation: 609

Two quick aliases for deleting all containers and images.

#Containers
$ alias rmdockerall="docker rm $(docker ps -a -q)"
#Images
$ alias rmidockerall="docker rmi $(docker images -a -q)"

Upvotes: 1

Vitalii Blagodir
Vitalii Blagodir

Reputation: 1099

It is not possible to overwrite docker image that has the same name in the name:tag format, but you could remove it automatically after the build if you set label my-label for the image my-image:latest:

docker build --tag my-image:latest --label my-label ubuntu:latest
docker image prune --force --filter='label=my-label'

Update: It is mandatory to follow strict sequence:

  1. docker build ...
  2. docker image prune ..
  3. docker build ...
  4. docker image prune ..

If you run:

  1. docker build ...

  2. docker image prune ..

  3. docker build ...

  4. docker build ...

  5. docker image prune ..

you will get image from step 3. not removed.

Upvotes: 8

jhm
jhm

Reputation: 4539

An easy way to clean up unused images and save disc space is to alias a cleanup command in your terminal by adding it to ~/.bashrc or ~/.bash_profile:

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

Then run docker_rmi_dangling in your shell.

(Inspiration from this comment)

Upvotes: 13

Usman Ismail
Usman Ismail

Reputation: 18679

You can use versions with your tags e/g/:

docker build -t <USER>/<CONTAINER>:<VERSION>   
docker build -t maluuba/haproxy:2
docker build -t maluuba/haproxy:latest  #Default behavior when you don't use version
docker build -t maluuba/haproxy:old

Upvotes: 2

Thomas Orozco
Thomas Orozco

Reputation: 55225

Images in Docker don't have a name, they have tags.

A tag is a reference to an image. Multiple tags may refer to the same image.

If you reassign a tag that is already used, then the original image will lose the tag, but will continue to exist (it will still be accessible by its image ID, and other tags might refer to it).

Upvotes: 85

Related Questions