REGEXVAULTv2.0
Dev & Systems/Docker
Verified Safe

Docker Image Full Reference Regex for PHP

/^(?:(?:[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 PHP. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.

Php Implementation

Php
<?php
// Docker Image Full Reference
// ReDoS-safe | RegexVault — Dev & Systems > Docker

define('DOCKER_IMAGE_FULL_REFERENCE_PATTERN', '/^(?:(?:[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}))?$/');

function validate_docker_image_full_reference(string $input): bool {
    return (bool) preg_match(DOCKER_IMAGE_FULL_REFERENCE_PATTERN, $input);
}

// Example
var_dump(validate_docker_image_full_reference("nginx")); // bool(true)

Test Cases

Matches (Valid)
Rejects (Invalid)
nginxNginx:latest
nginx:latestnginx: latest
ubuntu:22.04nginx:tag with space
library/nginx:alpineregistry/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 PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. 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