Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another attempt to implement saving multiple images #2050

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docker/api/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ def get_image(self, image, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
res = self._get(self._url("/images/{0}/get", image), stream=True)
return self._stream_raw_result(res, chunk_size, False)

@utils.check_resource('images')
def get_images(self, images, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
"""
Get a tarball of images. Similar to the ``docker save`` command.
Args:
images (str[]): Image names to get
chunk_size (int): The number of bytes returned by each iteration
of the generator. If ``None``, data will be streamed as it is
received. Default: 2 MB

Returns:
(generator): A stream of raw archive data.

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

Example:

>>> image = cli.get_images(["fedora:latest", "nginx:latest"])
>>> f = open('/tmp/fedora-and-nginx-latest.tar', 'w')
>>> for chunk in image:
>>> f.write(chunk)
>>> f.close()
"""
params = {
"names": images
}
res = self._get(self._url("/images/get"), params=params, stream=True)
return self._stream_raw_result(res, chunk_size, False)

@utils.check_resource('image')
def history(self, image):
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/api_image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,17 @@ def test_get_image(self):
timeout=DEFAULT_TIMEOUT_SECONDS
)

def test_get_images(self):
self.client.get_images([fake_api.FAKE_IMAGE_ID])

fake_request.assert_called_with(
'GET',
url_prefix + 'images/get',
stream=True,
params={"names": ["e9aa60c60128"]},
timeout=DEFAULT_TIMEOUT_SECONDS
)

def test_load_image(self):
self.client.load_image('Byte Stream....')

Expand Down
8 changes: 8 additions & 0 deletions tests/unit/fake_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ def get_fake_get_image():
return status_code, response


def get_fake_get_images():
status_code = 200
response = 'Byte Stream....'
return status_code, response


def post_fake_load_image():
status_code = 200
response = {'Id': FAKE_IMAGE_ID}
Expand Down Expand Up @@ -588,6 +594,8 @@ def post_fake_network_disconnect():
delete_fake_remove_image,
'{1}/{0}/images/e9aa60c60128/get'.format(CURRENT_VERSION, prefix):
get_fake_get_image,
'{1}/{0}/images/get'.format(CURRENT_VERSION, prefix):
get_fake_get_images,
'{1}/{0}/images/load'.format(CURRENT_VERSION, prefix):
post_fake_load_image,
'{1}/{0}/images/test_image/json'.format(CURRENT_VERSION, prefix):
Expand Down