diff --git a/gns3server/compute/docker/__init__.py b/gns3server/compute/docker/__init__.py index f65952d97..be6c1f960 100644 --- a/gns3server/compute/docker/__init__.py +++ b/gns3server/compute/docker/__init__.py @@ -25,6 +25,7 @@ import os import re import shutil import sys +from datetime import UTC, datetime import aiohttp import platformdirs @@ -69,6 +70,38 @@ GNS3_CONTAINER_NAME_RE = re.compile( # 'container 6d59f00236ff is using its referenced image d529dd0c6e55' CONFLICTING_CONTAINER_RE = re.compile(r"container ([0-9a-f]{12,64})\b") +# Images built for reproducibility, such as the Ubuntu images assembled with +# rockcraft, carry no created timestamp in their config and Docker reports 0 +# (displayed as 1970-01-01). Their build tool writes the real time in this +# OCI label instead. +OCI_CREATED_LABEL = "org.opencontainers.image.created" + + +def image_created_timestamp(image: dict) -> int: + """ + Build time of an image as a Unix timestamp. + + The daemon timestamp wins when it is set. When it is zero, fall back to + the OCI created label, if present. + + :param image: an image summary as returned by the daemon images API + """ + + created = image.get("Created") or 0 + if created > 0: + return created + label = (image.get("Labels") or {}).get(OCI_CREATED_LABEL, "") + if not label: + return created + try: + built = datetime.fromisoformat(label) + except ValueError: + log.warning(f"Ignoring invalid {OCI_CREATED_LABEL} label: {label!r}") + return created + if built.tzinfo is None: + built = built.replace(tzinfo=UTC) + return int(built.timestamp()) + class Docker(BaseManager): _NODE_CLASS = DockerVM @@ -594,7 +627,7 @@ class Docker(BaseManager): "image": tag, "id": image.get("Id"), "size": image.get("Size"), - "created": image.get("Created"), + "created": image_created_timestamp(image), } ) return sorted(images, key=lambda i: i["image"]) diff --git a/tests/compute/docker/test_docker.py b/tests/compute/docker/test_docker.py index deb58fea1..1d816d27f 100644 --- a/tests/compute/docker/test_docker.py +++ b/tests/compute/docker/test_docker.py @@ -149,6 +149,56 @@ async def test_list_images(): assert {"ubuntu:precise", "ubuntu:latest", "ubuntu:12.10", "ubuntu:quantal"} <= set(by_tag) +@pytest.mark.asyncio +async def test_list_images_created_fallback_to_oci_label(): + """ + Images built without a created timestamp (e.g. reproducible Ubuntu images) + report 0 from the daemon: the build time comes from the OCI created label. + """ + + response = [ + { + # zero timestamp, the label is used (Z suffix, nanosecond precision) + "RepoTags": ["ubuntu:latest"], + "Id": "febc1a2eec1f", + "Created": 0, + "Size": 78101592, + "Labels": {"org.opencontainers.image.created": "2026-10-25T10:26:07.538865123Z"}, + }, + { + # zero timestamp without a label stays at zero + "RepoTags": ["no-label:latest"], + "Id": "aaaaaa", + "Created": 0, + "Size": 1, + }, + { + # the daemon timestamp wins over the label when it is set + "RepoTags": ["normal:latest"], + "Id": "bbbbbb", + "Created": 1365714795, + "Size": 1, + "Labels": {"org.opencontainers.image.created": "2000-01-01T00:00:00Z"}, + }, + { + # a broken label is ignored, the timestamp stays at zero + "RepoTags": ["broken-label:latest"], + "Id": "cccccc", + "Created": 0, + "Size": 1, + "Labels": {"org.opencontainers.image.created": "not a date"}, + }, + ] + + with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response): + images = await Docker.instance().list_images() + by_tag = {image["image"]: image for image in images} + assert by_tag["ubuntu:latest"]["created"] == 1792923967 + assert by_tag["no-label:latest"]["created"] == 0 + assert by_tag["normal:latest"]["created"] == 1365714795 + assert by_tag["broken-label:latest"]["created"] == 0 + + @pytest.mark.asyncio async def test_pull_image():