22

I have a docker image generated from my build process. I am looking to tag the latest build image with tag build id and 'latest'. I see two ways to do this.

First approach - (Add Multiple tags and Push once)

docker tag <id> <user>/<image>:build_id  
docker tag <id> <user>/<image>:latest  
docker push <user>/<image>

Second - Tag individually and push

docker tag <id> <user>/<image>:build_id  
docker push <user>/<image>:build_id

docker tag <id> <user>/<image>:latest  
docker push <user>/<image>:latest

The docker documentation says if there is an image in the registry with a specific tag already, then docker push with a new image with same tag would overwrite the earlier image.

  1. Are both First and Second Option specified above functionally the same?
  2. Is there any preferred way/Best practice?
muru
  • 4,723
  • 1
  • 34
  • 78
Sandesh
  • 487
  • 1
  • 3
  • 8

2 Answers2

14

(About the original version of the question, which used docker push without arguments) docker push will not work unless you provide repository name.

$ docker push
"docker push" requires exactly 1 argument.
See 'docker push --help'.

Usage:  docker push [OPTIONS] NAME[:TAG] [flags]

Push an image or a repository to a registry

That means, you need to push with repository name. And you can either provide TAG or not.

If you do not provide TAG, you are pushing all images for that repository.


In first approach, you are pushing all images under <user>/<image> repository.

In second approach, you are pushing image one by one.

Answer of question

  1. Are both First and Second Option specified above functionally the same?

Both First and Second Option specified above are functionally the same (in your case).

If you do not provide TAG, you are pushing all images for that repository.

In your case

$ docker push <user>/<image>

will push both TAG build_id and latest

  1. Is there any preferred way/Best practice?

I think, second option is better and preferred

Because, you may not want to push all images. In that case, you can choose which image you want to push following second approach.

muru
  • 4,723
  • 1
  • 34
  • 78
Shahriar
  • 13,460
  • 8
  • 78
  • 95
  • "If you do not provide TAG, you are pushing all images for that repository." can anyone find a reference on this? I cannot find it in the official docs for Docker... – lindhe Oct 19 '20 at 13:27
  • `docker push --help` : Push an image or a repository to a registry – Shahriar Oct 19 '20 at 15:11
  • what version of Docker do you have? I'm at 19.03.6 and the help text for the push command is very sparse and mentions nothing about what images gets pushed if `TAG` is not provided. – lindhe Oct 25 '20 at 14:25
  • I am also confused if all tags are pushed if nothing is given what is "--all-tags" ("Push all tagged images in the repository") parameter for. https://docs.docker.com/engine/reference/commandline/push/ But maybe it depends on docker version. – timguy May 05 '22 at 13:04
0

It's also possible to tag the image during the build as stated in the docs

You can apply multiple tags to an image. For example, you can apply the latest tag to a newly built image and add another tag that references a specific version. For example, to tag an image both as whenry/fedora-jboss:latest and whenry/fedora-jboss:v2.1, use the following:

docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 .

and then push with -a | --all-tags as timguy commented before

til
  • 832
  • 11
  • 27