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.
What 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
Basic Label Syntax
Here's a practical example with common labels:
FROM node:20-alpine
LABEL 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 /app
COPY . .
RUN npm ci --only=production
CMD ["node", "index.js"]OCI Image Spec Labels
The Open Container Initiative (OCI) defines standard labels that should be used for consistency:
org.opencontainers.image.title- Human-readable titleorg.opencontainers.image.description- Description of the imageorg.opencontainers.image.version- Version of the packaged softwareorg.opencontainers.image.authors- Contact details of authorsorg.opencontainers.image.url- URL to get more informationorg.opencontainers.image.source- URL to source code repositoryorg.opencontainers.image.licenses- License(s) under which the image is distributed
Multi-Line Labels
You can combine multiple labels in a single instruction for cleaner Dockerfiles:
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"Viewing Labels
Use docker inspect to view labels on an image:
docker inspect my-app | grep Labels -A 10Use 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.
Best 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
Common 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!