Docker Image Tag Validation
Validates Docker image references like nginx:latest, myrepo/myimage:1.0.0, or registry.example.com/org/image:tag.
/^[a-z0-9]+(?:[._-][a-z0-9]+)*(?:\/[a-z0-9]+(?:[._-][a-z0-9]+)*)*(?::[a-zA-Z0-9._-]+)?$/How it works
Docker image names consist of an optional registry hostname, optional namespace/organization path, image name, and optional tag separated by a colon. This pattern validates the common formats used in Dockerfiles and docker-compose files.
Test Cases
Should Match
- nginx
- nginx:latest
- myorg/myimage:1.0.0
- registry.example.com/org/image:v2
Should NOT Match
- UPPERCASE/image
- image:tag:extra
- image with spaces
Quick Summary
Validates Docker image references including optional registry, namespace, image name, and tag. Image names must be lowercase. Tags can contain uppercase letters, digits, dots, underscores, and hyphens.
Key Takeaways
- Image name components must be lowercase alphanumeric with dots, underscores, or hyphens
- Tag (after :) can contain uppercase letters, digits, dots, underscores, and hyphens
- Registry hostname is part of the first path component if it contains a dot or colon
- No tag defaults to :latest in Docker, be explicit in production
When to use it
- Validating image references in CI/CD pipeline configurations
- Checking Dockerfile FROM instructions
- Sanitizing user-provided image names in container management UIs
Common Mistakes
- Using uppercase in image names, Docker Hub requires lowercase
- Omitting the tag in production, always pin to a specific version or digest
Docker Image Tag Validation, Frequently Asked
What is the difference between a tag and a digest?
A tag (e.g., :latest) is mutable, it can point to different images over time. A digest (e.g., @sha256:abc123) is immutable and always refers to the same image layer.
How do I pin to a specific image version?
Use a specific version tag (nginx:1.25.3) or a digest (nginx@sha256:...) instead of :latest.