267

How can several tags be attached to one Docker image? Is it possible to create multiple tags using one Dockerfile?

It is possible, somehow; for example docker pull ubuntu will get several images, some of which have multiple tags:

ubuntu                  13.10               9f676bd305a4        2 weeks ago         182.1 MB
ubuntu                  saucy               9f676bd305a4        2 weeks ago         182.1 MB
ubuntu                  raring              eb601b8965b8        2 weeks ago         170.2 MB
ubuntu                  13.04               eb601b8965b8        2 weeks ago         170.2 MB
ubuntu                  12.10               5ac751e8d623        2 weeks ago         161.4 MB
ubuntu                  quantal             5ac751e8d623        2 weeks ago         161.4 MB
ubuntu                  10.04               9cc9ea5ea540        2 weeks ago         183 MB
ubuntu                  lucid               9cc9ea5ea540        2 weeks ago         183 MB
ubuntu                  12.04               9cd978db300e        2 weeks ago         204.7 MB
ubuntu                  latest              9cd978db300e        2 weeks ago         204.7 MB
ubuntu                  precise             9cd978db300e        2 weeks ago         204.7 MB
F1Linux
  • 3,580
  • 3
  • 25
  • 24
Daniel YC Lin
  • 15,050
  • 18
  • 63
  • 96

4 Answers4

757

Since 1.10 release, you can now add multiple tags at once on build:

docker build -t name1:tag1 -t name1:tag2 -t name2 .

Source: Add ability to add multiple tags with docker build

Official Docker doc: https://docs.docker.com/engine/reference/commandline/build/#tag-an-image--t

Fidel Garcia
  • 405
  • 1
  • 6
  • 16
Marc Perrin-Pelletier
  • 12,696
  • 8
  • 28
  • 36
  • 14
    I think this is the more correct answer for ease of use in a build process – brasskazoo Oct 06 '16 at 00:08
  • This is not proper, because the ubuntu's tag for different version, the content is diffident. – Daniel YC Lin Sep 19 '17 at 09:14
  • 5
    How do I get these tags to appear in the repository after a push though? Unfortunately, only the one I use in the push command appears in the repo. – Czechnology Sep 19 '18 at 15:37
  • 8
    Even at the end of 2018 Docker is still incredibly immature in this respect, each 'tag' must *still* be pushed separately despite being attached to the same image. What is required here is some capability similar to Git tags. – Ed Randall Dec 29 '18 at 11:51
  • 2
    @EdRandall it doesn't matter because if the image already exist in your repo then docker just adds the tag to the image instead of pushing the entire image again. – Wadu Oct 25 '20 at 11:31
  • 1
    almost didn't upvote because I'd nudge it to 667 – Can Rau Jun 02 '22 at 08:23
  • You can now push with an --all-tags flag to push all tags at once. – giterdone Jun 03 '22 at 23:53
  • @giterdone it seems like `--all-tags` is going to push every single tag associated with an image, which may not actually be what somebody wanted to do after building with, say, 2 tags. – Stephen Sep 23 '22 at 21:41
120

You can't create tags with Dockerfiles but you can create multiple tags on your images via the command line.

Use this to list your image ids:

$ docker images

Then tag away:

$ docker tag 9f676bd305a4 ubuntu:13.10
$ docker tag 9f676bd305a4 ubuntu:saucy
$ docker tag eb601b8965b8 ubuntu:raring
...
David Braun
  • 5,573
  • 3
  • 36
  • 42
0

Tags must be scoped for repo:

I tried the accepted answer but my tags were ignored and just "latest" tag produced.

Reviewing the logs I saw an error was puked at the very end about lacking a scope, so realized that I needed to prepend my username before the name:tag:

The form should be:

-t <dockerhub uname>/name1:tag1 -t <dockerhub uname>/name1:tag2 etc...

ie: From the buildx command pushing to Docker Hub we'd tag as below:

docker buildx build --no-cache --platform linux/arm64 --push -t f1linux/postfix:3.7.2-r0 . -t f1linux/postfix:aarch64 -t f1linux/postfix:arm64
F1Linux
  • 3,580
  • 3
  • 25
  • 24
-16

HOW NOT TO DO IT!:

When building an image, you could also tag it this way.

docker build -t ubuntu:14.04 .

Then you build it again with another tag:

docker build -t ubuntu:latest .

If your Dockerfile makes good use of the cache, the same image should come out, and it effectively does the same as retagging the same image. If you do docker images then you will see that they have the same ID.

There's probably a case where this goes wrong though... But like @david-braun said, you can't create tags with Dockerfiles themselves, just with the docker command.

Sun
  • 2,110
  • 2
  • 21
  • 28
cassava
  • 578
  • 6
  • 14