3

Want to clarify my understanding as I am confused after studying the documentation

For command docker build -t the definition stated that

-t flag "Name and optionally a tag in the 'name:tag' format"

We then run docker tag xxx to "Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE" before we can push to the registry.

What is the difference of the functionality of 2 tagging here? Is the -t flag tags as the name of the build (image), which then docker tag that on top of the 'tagged' build before pushing it?

I tried searching online and questions here but couldn't find any explanation.

6
  • 1
    There is no difference between docker tag and the -t flag. The -t flag is just shorthand instead of building the image and running docker tag afterwards to tag the image id. Commented Jun 20, 2021 at 14:28
  • 1
    in short: docker build -t name:tag . == docker build . && docker tag <id_of_precedent_buid_written_on_screen> name:tag Commented Jun 20, 2021 at 14:29
  • thanks for clarifying, why is there many guides that specify both then? For example, AWS guide "Push commands for ECR" docs.aws.amazon.com/AmazonECR/latest/userguide/… and within console. Step3: Build the Docker image from your Dockerfile. "docker build -t hello-world ." and after that with "docker tag hello-world:latest aws_account_id.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest". Is there difference in purpose for the 2 steps? Commented Jun 20, 2021 at 14:55
  • does that mean if we use docker build -t flag, we can skip the command step docker tag? Commented Jun 20, 2021 at 15:07
  • 2
    Your example first tags the image with a name in your local registry (hello-world). Then it tags it with a name in a remote registry (aws_account_id.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest). That allows you to push the image to that registry. Commented Jun 20, 2021 at 15:45

1 Answer 1

6

With the help of (credits to) comments above and clear explanation in this video https://youtu.be/vWSRWpOPHws?t=354,

I cleared my doubts finally. docker build -t name:tag is to tag the image locally in docker. whereas using the tag command is renaming with other repo (other than dockerhub) for docker to recognize.. as mentioned by comments above and in the video.

I also tried a shortcut to verify if that works, so apparently we can shortcut with docker build -t <aws-ecr-uri-or-other-remote-repo-uri>/<same-name-as-your-app>:tag that combine both steps in one, then you can proceed to push it to AWS ECR with docker push

Just a note apart from the question. AWS ECR repo == 1 image each. (for different versions). So if you have different images, you have to create different repository in ECR for each.

Sign up to request clarification or add additional context in comments.

1 Comment

I was confused about this too as it is a bit different with ECR. The video helped to clarify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.