如何使用 docker-py 运行 docker tag

won*_*ton 4 dockerpy

在命令行上,您可以运行docker tag [from] [to]为图像指定另一个名称。该文档没有提供有关如何以编程方式执行此操作的任何信息。

如何使用 docker-py 进行 docker tag 操作?

won*_*ton 6

它不在文档中,但该Image对象有一个tag方法。

如果你跑

>>> dir(docker.from_env().images.get('my_image:latest'))
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'attrs', 'client', 'collection', 'history', 'id', 'id_attribute', 'labels', 'reload', 'save', 'short_id', 'tag', 'tags']
Run Code Online (Sandbox Code Playgroud)

奇怪的是有一个tag属性。

>>> docker.from_env().images.get('my_image:latest').tag
<bound method Image.tag of <Image: 'my_image:latest'>>
Run Code Online (Sandbox Code Playgroud)

运行它会产生:

>>> docker.from_env().images.get('my_image:latest').tag('my_image:foobar')
True
Run Code Online (Sandbox Code Playgroud)

在命令行上运行docker images表明标记操作成功完成。

docker.from_env().images.get('my_image:latest').tag.__doc__
Tag this image into a repository. Similar to the ``docker tag``
        command.

        Args:
            repository (str): The repository to set for the tag
            tag (str): The tag name
            force (bool): Force

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.

        Returns:
            (bool): ``True`` if successful
Run Code Online (Sandbox Code Playgroud)