如何使用docker-py列出给定图像的标签?

时间:2017-03-07 10:46:24

标签: docker docker-image dockerpy

说我有以下图片:

REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
docker.site.org:4000/project/my_backend    latest              d8f4b42c0cae        5 days ago          189 MB
docker.site.org:4000/project/my_backend    v1.6.1              d8f4b42c0cae        5 days ago          189 MB
docker.site.org:4000/project/my_daemon     latest              b8a28fa9fa9c        5 days ago          865 MB
docker.site.org:4000/project/my_daemon     v1.6.1              652e925c18f3        5 days ago          865 MB
docker.site.org:4000/project/my_daemon     v1.6.0              e6cff3d768d4        5 days ago          865 MB

问题

如何使用my_daemon列出docker-py图片的标签以获取此输出(订单无关紧要)?

['v1.6.1', 'v1.6.0', 'latest']

1 个答案:

答案 0 :(得分:0)

测试

def test_list_available_daemons_version(self):
    images = mock.get_images()

    versions = container_services.available_versions(images, name='project/my_daemon')

    self.assertEqual(len(versions), 3)
    self.assertCountEqual(set(versions), {'latest', 'v1.6.1', 'v1.6.0'})

解决方案

def available_versions(images=[], name='project/my_daemon'):
    versions = []

    for image in images:
        tags = image['RepoTags']
        for tag in tags:
            if name in tag:
                version = tag.split(':')[-1]
                versions.append(version)

    return versions