Module 3 Lesson 5: Image Tagging and Versioning
Master the art of naming your images. Learn the difference between repository and tag, and why relying on 'latest' is a major anti-pattern in production.
Module 3 Lesson 5: Image Tagging and Versioning
Naming your images correctly is the only way to stay sane as your project grows. Docker uses a specific format for naming: repository:tag.
1. Anatomy of an Image Name
my-username/my-app:v1.2.3
my-username: The Registry namespace (e.g., your Docker Hub username).my-app: The name of the image (The Repository).v1.2.3: The Tag (The Version).
2. Why "Latest" is a Trap
If you run docker build -t my-app ., Docker automatically gives it the tag :latest.
- The Problem: "Latest" just means "The last image built on this specific computer."
- The Nightmare: You build a version of your app on Monday. You deploy it. On Tuesday, you build a "Broken" version. Both are named
latest. You now have two different images with the same name, making it impossible to roll back safely.
Rule: Always use a version number or a "Git Commit Hash" as your tag in production.
3. Semantic Versioning (SemVer)
A common standard for tagging is Major.Minor.Patch (e.g., 2.3.1).
- Major: Breaking changes.
- Minor: New features (non-breaking).
- Patch: Bug fixes.
Multi-Tagging
Professional images often have multiple tags pointing to the same image:
my-app:2.3.1(Specific version)my-app:2.3(Latest minor version)my-app:2(Latest major version)
4. The docker tag Command
You don't need to rebuild an image to rename it. You can just add a new "Alias" to an existing image ID.
# docker tag <old-name> <new-name>
docker tag my-local-app:latest my-hub-user/production-app:v1.0.1
Once tagged, you can share it with the world:
docker push my-hub-user/production-app:v1.0.1
Exercise: The Tagging Strategy
- Imagine you have a bug fix for Version 1.5.2. What should the new tag be?
- Command: Tag the local image
nginx:latestwith the namemycompany/custom-nginx:retail-prod-v1. - Why do many companies tag their images with the current date (e.g.,
app:2024-12-21)? What are the pros and cons of this?
Conclusion of Module 3
You are now a Container Builder. You know how to write a Dockerfile, optimize for caching, follow security best practices, and name your versions properly.
Next Module: We look at where the data lives: Container Management (Networking and Volumes).