Is there a way to obtain the date when the docker image was created using docker API for python

1.3k views Asked by At

I would like to obtain the created at date for a docker image using docker API. Is this possible? I don't see it in the docs https://docker-py.readthedocs.io/en/stable/images.html. I see a way to obtain it using requests but was hoping to use docker API as my code uses docker API to grab other information such as Registry ID.

import docker
cli=docker.from_env()
cl_image = cli.images.get_registry_data(reg_url, auth_config=None)
image_hash = cl_image.id

2

There are 2 answers

4
AzyCrw4282 On

No, getting the creation date is not supported by the Docker SDK for python. The create attribute simply doesn't exist so you will not be able to get that value. So you will have to use the request module to fetch the data from Docker API.

Note: Your import library should not be referred to as API it is simply a library supporting the Docker Engine API. The real API is here which you'll use it to make a GET request.

Edit:

I am not sure if you are doing your authentication correctly. You need to provide credentials with values and encode in base64url (JSON) and pass them as X_Registry-Auth header in your request call, see this. This example perfectly illustrates what you have to do, albeit it's shown in the context of cURL POST request.

0
austin1howard On

The image creation timestamp is on the Image object, under the attrs property:

from dateutil.parser import isoparse
import docker

client = docker.from_env()

images = client.images.list()
img = images[0]

created_str = img.attrs["Created"]
created_datetime = isoparse(created_str)
print(created_datetime)