Docker Image Full Reference Regex for Python
/^(?:(?:[a-zA-Z0-9][a-zA-Z0-9\-._]*(?::[0-9]+)?)/)?(?:[a-z][a-z0-9\-._]*/)?[a-z][a-z0-9\-._]*(?::([a-zA-Z0-9][a-zA-Z0-9._\-]{0,127})|@(sha256:[a-fA-F0-9]{64}))?$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching docker image full reference, ported and verified for Python. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.
Python Implementation
# Docker Image Full Reference
# ReDoS-safe | RegexVault — Dev & Systems > Docker
import re
docker_image_full_reference_pattern = re.compile(r'^(?:(?:[a-zA-Z0-9][a-zA-Z0-9\-._]*(?::[0-9]+)?)/)?(?:[a-z][a-z0-9\-._]*/)?[a-z][a-z0-9\-._]*(?::([a-zA-Z0-9][a-zA-Z0-9._\-]{0,127})|@(sha256:[a-fA-F0-9]{64}))?$')
def validate_docker_image_full_reference(value: str) -> bool:
return bool(docker_image_full_reference_pattern.fullmatch(value))
# Example
print(validate_docker_image_full_reference("nginx")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
nginx | Nginx:latest |
nginx:latest | nginx: latest |
ubuntu:22.04 | nginx:tag with space |
library/nginx:alpine | registry/Image:tag |
myregistry.com:5000/myimage:v1.0 | — |
nginx@sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4 | — |
When to use this pattern
This pattern is drawn from the Dev & Systems > Docker category and carries a ReDoS-safe certification. That matters for Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
A registry hostname must contain a period or colon or be 'localhost' to distinguish it from a namespace — 'myregistry/image' is parsed as namespace/image on Docker Hub.
Technical Notes
Group 1 = tag, group 2 = digest. Docker resolves bare names like 'nginx' to 'docker.io/library/nginx:latest'. Always specify the full reference in IaC and CI/CD pipelines.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern