Docker Image Python Parser
docker-image-py is a Python library designed to parse Docker image names, tags, and registries, mimicking how Docker distributions handle image parsing. It provides a structured way to break down an image string into its components. The current version is 0.1.13, released in July 2024, and the library maintains an active, albeit infrequent, release cadence, driven by community contributions and specific parsing needs.
Warnings
- gotcha This library is 'docker-image-py' (from realityone), which specifically focuses on *parsing* Docker image names. It is not the 'docker-py' or 'docker' SDK (from Docker Inc.) which is used to interact with the Docker daemon API to build, run, and manage containers. Confusing the two can lead to incorrect imports and unexpected behavior.
- gotcha The `Reference.parse()` method will infer 'docker.io' as the registry if no registry is explicitly provided in the image string. Be aware of this default behavior, especially when working with private or custom registries where an explicit registry name is always expected.
- gotcha The library primarily handles the structural parsing of image names. It does not validate the existence of the image, the correctness of the digest (beyond format), or the accessibility of the registry. Malformed image strings may raise exceptions or produce unexpected parsing results.
Install
-
pip install docker-image-py
Imports
- Reference
from docker_image import Reference
Quickstart
from docker_image import Reference
# Parse a full image reference
ref = Reference.parse('registry.example.com/repo/image:tag')
print(f"Registry: {ref.registry}")
print(f"Repository: {ref.repository}")
print(f"Tag: {ref.tag}")
print(f"Digest: {ref.digest}")
print(f"Raw: {ref.raw}")
# Parse an image with no registry specified (defaults to 'docker.io')
ref_default = Reference.parse('ubuntu:latest')
print(f"\nRegistry (default): {ref_default.registry}")
print(f"Repository: {ref_default.repository}")
# Parse an image with a digest
ref_digest = Reference.parse('myrepo/myimage@sha256:456abc...')
print(f"\nRepository: {ref_digest.repository}")
print(f"Digest: {ref_digest.digest}")