Skip to main content

Docker Tags

Introduction

Docker tags are an essential component of Docker's image naming and versioning system. They allow you to identify specific versions or variations of Docker images, making your containerization workflow more manageable and predictable. In this guide, we'll explore what Docker tags are, how they work, and how to use them effectively in your projects.

What Are Docker Tags?

A Docker tag is a label or identifier applied to Docker images. Think of tags as a way to specify which version or variant of an image you want to use. The full reference to a Docker image typically follows this format:

registry/repository:tag

For example:

docker.io/nginx:1.21.0

Where:

  • docker.io is the registry (Docker Hub in this case)
  • nginx is the repository name
  • 1.21.0 is the tag

If you don't specify a tag when pulling or referencing an image, Docker uses the default tag latest.

How Docker Tags Work

Basic Tag Format

Tags are alphanumeric strings that can include underscores (_), periods (.), and hyphens (-). They cannot contain uppercase letters by default, as Docker converts them to lowercase.

The Default latest Tag

When you don't specify a tag, Docker automatically uses the latest tag:

bash
# These two commands are equivalent
docker pull nginx
docker pull nginx:latest
caution

The latest tag doesn't necessarily mean the newest version! It's simply the default tag that image publishers assign, which may or may not be the most recent version.

Creating and Managing Docker Tags

Tagging Images Locally

You can tag Docker images you've built or already pulled using the docker tag command:

bash
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Example:

bash
# Tag an existing image with a new tag
docker tag nginx:latest my-nginx:1.0

# Output: (no output means it worked)

After running this command, you'll have two references to the same image:

  • nginx:latest
  • my-nginx:1.0

You can verify this with:

bash
docker images

# Example output:
# REPOSITORY TAG IMAGE ID CREATED SIZE
# nginx latest 04661cdce581 2 weeks ago 142MB
# my-nginx 1.0 04661cdce581 2 weeks ago 142MB

Notice how both images have the same IMAGE ID, indicating they're the same image with different tags.

Tagging During the Build Process

You can also apply tags when building a Docker image:

bash
docker build -t repository:tag .

Example:

bash
docker build -t my-app:1.0 .

# Output: (build output omitted)
# Successfully built 3a7c23f4e0ab
# Successfully tagged my-app:1.0

Multiple Tags for the Same Image

You can assign multiple tags to the same image:

bash
docker build -t my-app:1.0 -t my-app:latest .

This is a common practice when releasing new versions while also updating the latest reference.

Common Tagging Strategies

Semantic Versioning

One of the most popular tagging strategies follows Semantic Versioning (SemVer) principles:

Major.Minor.Patch

Examples:

  • 1.0.0 - Initial release
  • 1.1.0 - Added new features (Minor update)
  • 1.1.1 - Bug fixes (Patch update)
  • 2.0.0 - Breaking changes (Major update)

Date-Based Tags

Some teams prefer date-based tagging:

YYYY-MM-DD

Example:

my-app:2023-04-15

Git Hash-Based Tags

Using Git commit hashes creates uniquely identifiable images:

my-app:git-7b36a406

Environment or Stage-Based Tags

Tags can indicate the intended environment:

my-app:production
my-app:staging
my-app:development

Practical Examples

Example 1: Managing a Web Application Across Environments

Let's say you're developing a web application and want to manage different versions:

bash
# Build production version
docker build -t my-webapp:1.0.0 -t my-webapp:latest -t my-webapp:production .

# Later, build a development version with new features
docker build -f Dockerfile.dev -t my-webapp:1.1.0-dev -t my-webapp:development .

This approach lets you:

  • Keep a stable production image (my-webapp:production, my-webapp:1.0.0)
  • Maintain an easy reference to the latest stable release (my-webapp:latest)
  • Have a separate development version (my-webapp:development, my-webapp:1.1.0-dev)

Example 2: Working with Different Base Images

Tags allow you to maintain variants of your application based on different base images:

dockerfile
# Dockerfile.alpine
FROM node:16-alpine
# ... rest of your Dockerfile
dockerfile
# Dockerfile.debian
FROM node:16
# ... rest of your Dockerfile

Build commands:

bash
docker build -f Dockerfile.alpine -t my-api:1.0-alpine .
docker build -f Dockerfile.debian -t my-api:1.0-debian .

This gives you flexibility to choose between a smaller Alpine-based image or a more feature-rich Debian-based image.

Working with Tags in Docker Compose

In a docker-compose.yml file, you can specify exact image tags:

yaml
version: '3.8'
services:
webapp:
image: nginx:1.21.0
database:
image: postgres:13.3

This ensures your compose setup uses specific versions, making your environment more consistent and predictable.

Tag Best Practices

  1. Never rely solely on the latest tag for production

    • It's unpredictable and can change without warning
  2. Use specific version tags for production environments

    • Ensures consistency and reproducibility
  3. Document your tagging strategy

    • Create a README or documentation explaining your tag conventions
  4. Consider using immutable tags

    • Once an image is tagged, that tag should never be reused for a different image
  5. Use descriptive tags

    • Tags should communicate meaning about the image's contents or purpose
  6. Implement automated tagging in CI/CD pipelines

    • Automation reduces human error in the tagging process

Viewing Available Tags

You can view all available tags for an image on Docker Hub or use the Docker API:

bash
# Using curl to check available nginx tags
curl -s https://registry.hub.docker.com/v2/repositories/library/nginx/tags | grep -o '"name":"[^"]*' | grep -o '[^"]*$' | head -n 5

# Example output:
# latest
# stable
# 1.23.3
# 1.23
# 1

Pushing Tagged Images to a Registry

After tagging your images, you can push them to a registry:

bash
# First, tag the image for your registry
docker tag my-app:1.0 username/my-app:1.0

# Then push it
docker push username/my-app:1.0

# Output:
# The push refers to repository [docker.io/username/my-app]
# 5f70bf18a086: Pushed
# ...
# 1.0: digest: sha256:1234... size: 1234

Summary

Docker tags are powerful tools for managing container images throughout your development lifecycle. They help you:

  • Identify specific versions of images
  • Manage different variants of the same application
  • Implement versioning strategies
  • Ensure consistency across environments

Understanding how to effectively use tags is essential for creating maintainable, predictable Docker-based applications.

Additional Resources

Exercises

  1. Basic Tagging: Create a simple Dockerfile, build it, and tag it with three different tags: your name, today's date, and a version number.

  2. Multi-stage Builds with Tags: Create a multi-stage Dockerfile that builds a simple web application. Tag the final image with both a version tag and an environment tag.

  3. Tag Strategy Implementation: Design a tagging strategy for a hypothetical application that has web, API, and database components. Document how you would tag each component through development, testing, and production stages.



If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)