Atharva Unde
~/writing/ dockerfile-labels-a-comprehensive-guide

Dockerfile Labels: A Comprehensive Guide

Dockerfile Labels Guide: Metadata, versioning, maintainer info, and organizational best practices for Docker images and container orchestration.

Atharva UndeFeb 5, 2024 ยท 10 min readDocker

Labels are key-value pairs that add metadata to your Docker images. While they don't impact image size significantly, they're crucial for documentation, automation, and integration with orchestration tools.

01What Are Dockerfile Labels?

Labels allow you to attach arbitrary metadata to Docker images. This metadata can include:

  • Version information
  • Maintainer details
  • Build dates and commit hashes
  • License information
  • Custom application metadata

02Basic Label Syntax

Here's a practical example with common labels:

dockerfile
FROM node:24-alpineLABEL maintainer="atharva@example.com"LABEL version="1.0.0"LABEL description="My awesome Node.js application"LABEL org.opencontainers.image.title="my-app"LABEL org.opencontainers.image.description="Production-ready Node.js app"LABEL org.opencontainers.image.version="1.0.0"LABEL org.opencontainers.image.authors="Atharva Unde" WORKDIR /appCOPY . .RUN npm ci --only=productionCMD ["node", "index.js"]

03OCI Image Spec Labels

The Open Container Initiative (OCI) defines standard labels that should be used for consistency:

  • org.opencontainers.image.title - Human-readable title
  • org.opencontainers.image.description - Description of the image
  • org.opencontainers.image.version - Version of the packaged software
  • org.opencontainers.image.authors - Contact details of authors
  • org.opencontainers.image.url - URL to get more information
  • org.opencontainers.image.source - URL to source code repository
  • org.opencontainers.image.licenses - License(s) under which the image is distributed

04Multi-Line Labels

You can combine multiple labels in a single instruction for cleaner Dockerfiles:

dockerfile
LABEL org.opencontainers.image.title="my-app" \      org.opencontainers.image.description="Production app" \      org.opencontainers.image.version="1.0.0" \      org.opencontainers.image.authors="Atharva Unde"

05Viewing Labels

Use docker inspect to view labels on an image:

bash
docker inspect my-app | grep Labels -A 10

06Use Cases for Labels

1. Documentation

Labels provide self-documenting images. Anyone can inspect an image and understand its purpose, version, and maintainer.

2. Automation

CI/CD pipelines can read labels to make decisions about deployments, notifications, or cleanup policies.

3. Orchestration

Kubernetes and other orchestration tools can use labels for scheduling, affinity rules, and service discovery.

4. Compliance and Auditing

Track which images are deployed, their versions, and licensing information for compliance requirements.

07Best Practices

  • Use the OCI standard label namespace for common metadata
  • Create your own namespace for custom labels (e.g., com.mycompany.*)
  • Keep label values concise and meaningful
  • Use reverse-DNS notation for namespacing (com.example.app.*)
  • Document your custom labels in your project README
  • Automate label population in CI/CD (e.g., git commit hash, build date)

Pro Tip: Use build arguments with labels to inject dynamic values like git commit hashes: LABEL git.commit=$GIT_COMMIT

08Common Pitfalls

  • Don't store sensitive information in labels (they're visible to anyone)
  • Avoid excessively long label values
  • Don't use labels for runtime configuration (use environment variables instead)

In the final article of this series, we'll explore advanced optimization with Docker layers, caching, and multi-stage builds. Stay tuned!


DockerDockerfilelabelscontainer metadataDocker image metadatalabel best practicescontainer versioningmaintainer labelsKubernetes labels

Keep reading

All posts