Docker Image Full Reference Regex for Go
/^(?:(?:[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 Go. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Go project — whether you're validating in a Gin handler, a gRPC service, or a command-line tool.
Go Implementation
// Docker Image Full Reference
// ReDoS-safe | RegexVault — Dev & Systems > Docker
package validation
import "regexp"
var dockerImageFullReferenceRe = regexp.MustCompile(`^(?:(?:[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}))?$`)
func ValidateDockerImageFullReference(s string) bool {
return dockerImageFullReferenceRe.MatchString(s)
}
// Example
// fmt.Println(ValidateDockerImageFullReference("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 Go developers because Go's RE2 engine is inherently safe from catastrophic backtracking, but this pattern has been additionally verified for correctness. 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