I need a Javascript regex pattern to test the Docker Image URL.
According to my research, the Docker image URL pattern is:
<registry>/<repository>/<image>:<tag>
Here are the components explained:
registry (optional): The URL of the registry where the Docker image is hosted. This is optional, and if not provided, Docker assumes the image is on Docker Hub. Examples include docker.io, gcr.io, or a custom registry URL.
repository (optional): The repository is a grouping for related Docker images. This is optional, and if not provided, it implies the “library” namespace on Docker Hub. Examples include nginx, ubuntu, or a custom repository.
image: The name of the Docker image within the repository. Examples include nginx, ubuntu, or a custom image name.
tag (optional): The tag specifies a particular version or variant of the image. This is optional, and if not provided, Docker assumes the “latest” tag. Examples include **latest**, 1.0, v1, 020921, or a custom tag.
Here are some common patterns for Docker image URLs with related test cases (examples) that should check with Javascript regex:
1. Docker Hub:
<image>
<user>/<image>
<user>/<repository>/<image>
<registry>/<user>/<repository>/<image>
Example:
nginx
username/nginx
username/myrepo/nginx
registry.example.com/username/myrepo/nginx
2. Private Registry:
<registry>/<image>
<registry>/<repository>/<image>
Example:
registry.example.com/myimage
registry.example.com/myrepo/myimage
3. With Tag:
<image>:<tag>
<user>/<image>:<tag>
<registry>/<user>/<repository>/<image>:<tag>
Example:
nginx:latest
username/nginx:1.0
registry.example.com/username/myrepo/nginx:2.0
4. Digest:
<image>@<digest>
<registry>/<image>@<digest>
Example:
nginx@sha256:abc123def456...
registry.example.com/myimage@sha256:xyz789uvw123...
5. URLs for Specific Registries:
quay.io/<user>/<image>
ghcr.io/<user>/<repository>/<image>
Example:
quay.io/username/myimage
ghcr.io/username/myrepo/myimage
6. Local Images:
<image>:<tag>
<repository>/<image>:<tag>
Example:
myimage:latest
myrepo/myimage:1.0
Actually I’ve tried following regex to test Docker image URLs:
const dockerImage = /^(?<repository>[w.-_]+((?::d+|)(?=/[a-z0-9._-]+/[a-z0-9._-]+))|)(?:/|)(?<image>[a-z0-9.-_]+(?:/[a-z0-9.-_]+|))(:(?<tag>[w.-_]{1,127})|)$/gim;
But it didn’t work properly in following situations:
dockerImage.test('http://example.example.com/ai-ocr:v1') //output: false , expected: true
dockerImage.test('example.example.com/crazymax/yasu') //output: false , expected: true
And it worked properly in this cases:
dockerImage.test('example.example.com/face-anonymizer:020921') //output: true , expected: true
dockerImage.test('example.example.com/operation_service_images/mohammad-mohammad') //output: true , expected: true