Sub topic 5
Naming and Tagging an Image with -t
This chapter assumes you can write a basic Dockerfile and run docker build with a build context. It covers how an image name is structured and how the -t flag applies names and tags.
Anatomy of an image name
An image reference has up to four parts:
registry.example.com/team/myapp:1.0registry.example.comis the registry host. If you leave it out, Docker assumes Docker Hub.teamis the namespace, such as a user or organization.myappis the repository name.1.0is the tag, a label that points to one specific image.
If you leave out the tag, Docker uses latest. A name like myapp therefore means myapp:latest.
A tag is a pointer, not a copy. Several tags can point at the same image ID, and a tag can later be moved to a different image.
Applying tags with -t
The -t (or --tag) flag names the image at build time. Repeat it to apply several tags to the same image.
docker build -t myapp:1.0 -t myapp:latest .The final . is the build context, not part of the name. Both tags refer to one image, so the build runs once and nothing is duplicated on disk.
Without -t, the build still succeeds, but the image has no name. It shows up as <none> in listings and you can only refer to it by ID.
Verifying the tags
List the repository to see every tag and the image each one points to.
docker image ls myappThe output looks like this, with a shortened image ID:
REPOSITORY TAG IMAGE ID CREATED SIZE
myapp 1.0 3f1c2a9d8b7e 10 seconds ago 130MB
myapp latest 3f1c2a9d8b7e 10 seconds ago 130MBIdentical IDs in the IMAGE ID column confirm that the two tags share one image.
Naming rules
Docker rejects names that break these rules:
- Repository names must be lowercase. They may contain letters, digits, and the separators
.,_, and-. - Tags may contain letters, digits,
_,., and-. They cannot start with.or-and can be at most 128 characters. - A tag cannot contain
/or:.
Rebuilding moves tags
When you build again with a tag that already exists, Docker moves the tag to the new image. The old image keeps its content but loses the name, so it appears as <none>.
docker build -t myapp:latest .
docker image lsAfter a rebuild that changed something, the previous myapp:latest image is listed as <none>:<none>. This is called a dangling image. It still uses disk space until you remove it.
Common errors
- Uppercase letters in the name.
docker build -t MyApp .fails with an "invalid reference format" error. Usemyapp. - Missing context.
docker build -t myappfails because no path was given. Add.at the end. - Assuming
latestis the newest build.latestis only a default tag. It points to whatever image was last given that tag, and you must apply it yourself. - Putting a registry in the name too late. If you build as
myapp:1.0and later needregistry.example.com/team/myapp:1.0, you do not need to rebuild. A later chapter shows how to add names withdocker tag.
Exercise
You run docker build -t myapp:2.0 -t myapp:stable . and then docker image ls myapp. How many images does Docker store, and what do you expect to see in the IMAGE ID column?
Show answer
Docker stores one image. Both rows, myapp:2.0 and myapp:stable, show the same IMAGE ID because the two tags point to the same image.
Exercise
A teammate runs docker build -t MyApp:1.0 . and gets an error. What is wrong, and what is the fix?
Show answer
Repository names must be lowercase. Change the name to myapp:1.0.