4

On the command line, you can run docker tag [from] [to] to give an image another name. The documentation does not provide any information on how to do this programmatically.

How do you use docker-py to do a docker tag operation?

wonton
  • 7,568
  • 9
  • 56
  • 93

1 Answers1

6

It's not in the documentation but the Image object has a tag method.

If you run

>>> 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']

Curiously there is a tag attribute.

>>> docker.from_env().images.get('my_image:latest').tag
<bound method Image.tag of <Image: 'my_image:latest'>>

Running it yields:

>>> docker.from_env().images.get('my_image:latest').tag('my_image:foobar')
True

Running docker images on the command line shows that the tag operation went through successfully.

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
wonton
  • 7,568
  • 9
  • 56
  • 93