75

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

nbro
  • 15,395
  • 32
  • 113
  • 196
lolski
  • 16,231
  • 7
  • 34
  • 49

6 Answers6

75

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).

Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • 4
    They have both a name and a tag. You can verify that below. https://docs.docker.com/v17.09/engine/reference/commandline/build/#options – Gi0rgi0s Jun 27 '19 at 14:23
  • 5
    @Gi0rgi0s The terminology used by Docker can be a little inconsistent. "Tag" sometimes references the `name:tag` combo, and sometimes just the latter part of it. That said, in the context of building a Docker image (i.e. in the context of this question), the `--tag` argument does reference the whole combo, not just the trailing part after the `:` (that said, from your comment, I'm not certain what point you're trying to make). – Thomas Orozco Jun 30 '19 at 13:43
  • I'm not trying to make a point. However I do think it would be useful if you added the context from your comment into your answer. – Gi0rgi0s Jul 01 '19 at 18:26
  • They have both, name and tag and thus they have made sure to make it perfectly confusing – Atul Aug 27 '22 at 08:16
11

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)

CvRChameleon
  • 367
  • 1
  • 5
  • 29
jhm
  • 4,379
  • 5
  • 33
  • 49
5

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.

  • 1
    This doesn't work when giving the same tag as the old image was. The old image will be a unremovable parent image. – burny Aug 16 '20 at 21:36
  • You should 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. Updated my answer. – Vitalii Blagodir Aug 18 '20 at 13:23
3

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
Usman Ismail
  • 17,999
  • 14
  • 83
  • 165
2

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.

Kalpit
  • 171
  • 1
  • 5
1

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)"
hpl002
  • 530
  • 4
  • 7