2

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

py_noob
  • 433
  • 2
  • 8
  • 17

2 Answers2

3

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)
austin1howard
  • 4,815
  • 3
  • 20
  • 23
1

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.

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • Thanks. I'm unsure how to implement it, trying it out. If you could share a snippet with any public docker image, that would be helpful. – py_noob Sep 16 '20 at 16:13
  • @py_noob I have updated the URL, which points to the right steps you need to take. Configure your call with the required parameters and it should give you the response shown in the docs – AzyCrw4282 Sep 16 '20 at 17:30
  • Thanks a lot! Much appreciated. I however get a for every url I provide so trying to debug that. Here's the code snippet: `rfinal=requests.get('https://gitlab-registry.nameofprogram.com/abc/source_repo/', headers={"PRIVATE_TOKEN": })` – py_noob Sep 16 '20 at 18:16
  • For anyone finding this in the future, the python docker library supports this (and possibly in 2020 also?), under the `attrs` of an image object. – austin1howard Jul 08 '22 at 21:07