Skip to content

Commit cec152d

Browse files
author
aiordache
committed
Set image default tag on pull
Signed-off-by: aiordache <[email protected]>
1 parent ed46fb0 commit cec152d

File tree

7 files changed

+45
-23
lines changed

7 files changed

+45
-23
lines changed

docker/api/image.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def prune_images(self, filters=None):
343343
return self._result(self._post(url, params=params), True)
344344

345345
def pull(self, repository, tag=None, stream=False, auth_config=None,
346-
decode=False, platform=None):
346+
decode=False, platform=None, all_tags=False):
347347
"""
348348
Pulls an image. Similar to the ``docker pull`` command.
349349
@@ -358,6 +358,7 @@ def pull(self, repository, tag=None, stream=False, auth_config=None,
358358
decode (bool): Decode the JSON data from the server into dicts.
359359
Only applies with ``stream=True``
360360
platform (str): Platform in the format ``os[/arch[/variant]]``
361+
all_tags (bool): Pull all image tags.
361362
362363
Returns:
363364
(generator or str): The output
@@ -382,8 +383,12 @@ def pull(self, repository, tag=None, stream=False, auth_config=None,
382383
}
383384
384385
"""
385-
if not tag:
386-
repository, tag = utils.parse_repository_tag(repository)
386+
repository, image_tag = utils.parse_repository_tag(repository)
387+
tag = tag or image_tag or 'latest'
388+
389+
if all_tags:
390+
tag = None
391+
387392
registry, repo_name = auth.resolve_repository_name(repository)
388393

389394
params = {

docker/models/images.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,12 @@ def load(self, data):
395395

396396
return [self.get(i) for i in images]
397397

398-
def pull(self, repository, tag=None, **kwargs):
398+
def pull(self, repository, tag=None, all_tags=False, **kwargs):
399399
"""
400400
Pull an image of the given name and return it. Similar to the
401401
``docker pull`` command.
402-
If no tag is specified, all tags from that repository will be
403-
pulled.
402+
If ``all_tags`` is set, the ``tag`` parameter is ignored and all image
403+
tags will be pulled.
404404
405405
If you want to get the raw pull output, use the
406406
:py:meth:`~docker.api.image.ImageApiMixin.pull` method in the
@@ -413,10 +413,11 @@ def pull(self, repository, tag=None, **kwargs):
413413
config for this request. ``auth_config`` should contain the
414414
``username`` and ``password`` keys to be valid.
415415
platform (str): Platform in the format ``os[/arch[/variant]]``
416+
all_tags (bool): Pull all image tags
416417
417418
Returns:
418419
(:py:class:`Image` or list): The image that has been pulled.
419-
If no ``tag`` was specified, the method will return a list
420+
If ``tag`` is None, the method will return a list
420421
of :py:class:`Image` objects belonging to this repository.
421422
422423
Raises:
@@ -426,13 +427,13 @@ def pull(self, repository, tag=None, **kwargs):
426427
Example:
427428
428429
>>> # Pull the image tagged `latest` in the busybox repo
429-
>>> image = client.images.pull('busybox:latest')
430+
>>> image = client.images.pull('busybox')
430431
431432
>>> # Pull all tags in the busybox repo
432-
>>> images = client.images.pull('busybox')
433+
>>> images = client.images.pull('busybox', all_tags=True)
433434
"""
434-
if not tag:
435-
repository, tag = parse_repository_tag(repository)
435+
repository, image_tag = parse_repository_tag(repository)
436+
tag = tag or image_tag or 'latest'
436437

437438
if 'stream' in kwargs:
438439
warnings.warn(
@@ -442,14 +443,14 @@ def pull(self, repository, tag=None, **kwargs):
442443
del kwargs['stream']
443444

444445
pull_log = self.client.api.pull(
445-
repository, tag=tag, stream=True, **kwargs
446+
repository, tag=tag, stream=True, all_tags=all_tags, **kwargs
446447
)
447448
for _ in pull_log:
448449
# We don't do anything with the logs, but we need
449450
# to keep the connection alive and wait for the image
450451
# to be pulled.
451452
pass
452-
if tag:
453+
if not all_tags:
453454
return self.get('{0}{2}{1}'.format(
454455
repository, tag, '@' if tag.startswith('sha256:') else ':'
455456
))

tests/integration/api_image_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_pull(self):
4242
self.client.remove_image('hello-world')
4343
except docker.errors.APIError:
4444
pass
45-
res = self.client.pull('hello-world', tag='latest')
45+
res = self.client.pull('hello-world')
4646
self.tmp_imgs.append('hello-world')
4747
assert type(res) == six.text_type
4848
assert len(self.client.images('hello-world')) >= 1
@@ -55,7 +55,7 @@ def test_pull_streaming(self):
5555
except docker.errors.APIError:
5656
pass
5757
stream = self.client.pull(
58-
'hello-world', tag='latest', stream=True, decode=True)
58+
'hello-world', stream=True, decode=True)
5959
self.tmp_imgs.append('hello-world')
6060
for chunk in stream:
6161
assert isinstance(chunk, dict)

tests/integration/models_images_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_pull_with_sha(self):
8686

8787
def test_pull_multiple(self):
8888
client = docker.from_env(version=TEST_API_VERSION)
89-
images = client.images.pull('hello-world')
89+
images = client.images.pull('hello-world', all_tags=True)
9090
assert len(images) >= 1
9191
assert any([
9292
'hello-world:latest' in img.attrs['RepoTags'] for img in images

tests/unit/api_image_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_pull(self):
6767
args = fake_request.call_args
6868
assert args[0][1] == url_prefix + 'images/create'
6969
assert args[1]['params'] == {
70-
'tag': None, 'fromImage': 'joffrey/test001'
70+
'tag': 'latest', 'fromImage': 'joffrey/test001'
7171
}
7272
assert not args[1]['stream']
7373

@@ -77,7 +77,7 @@ def test_pull_stream(self):
7777
args = fake_request.call_args
7878
assert args[0][1] == url_prefix + 'images/create'
7979
assert args[1]['params'] == {
80-
'tag': None, 'fromImage': 'joffrey/test001'
80+
'tag': 'latest', 'fromImage': 'joffrey/test001'
8181
}
8282
assert args[1]['stream']
8383

tests/unit/models_containers_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def test_run_pull(self):
233233

234234
assert container.id == FAKE_CONTAINER_ID
235235
client.api.pull.assert_called_with(
236-
'alpine', platform=None, tag=None, stream=True
236+
'alpine', platform=None, tag='latest', all_tags=False, stream=True
237237
)
238238

239239
def test_run_with_error(self):

tests/unit/models_images_test.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,35 @@ def test_load(self):
4444

4545
def test_pull(self):
4646
client = make_fake_client()
47-
image = client.images.pull('test_image:latest')
47+
image = client.images.pull('test_image:test')
4848
client.api.pull.assert_called_with(
49-
'test_image', tag='latest', stream=True
49+
'test_image', tag='test', all_tags=False, stream=True
50+
)
51+
client.api.inspect_image.assert_called_with('test_image:test')
52+
assert isinstance(image, Image)
53+
assert image.id == FAKE_IMAGE_ID
54+
55+
def test_pull_tag_precedence(self):
56+
client = make_fake_client()
57+
image = client.images.pull('test_image:latest', tag='test')
58+
client.api.pull.assert_called_with(
59+
'test_image', tag='test', all_tags=False, stream=True
60+
)
61+
client.api.inspect_image.assert_called_with('test_image:test')
62+
63+
image = client.images.pull('test_image')
64+
client.api.pull.assert_called_with(
65+
'test_image', tag='latest', all_tags=False, stream=True
5066
)
5167
client.api.inspect_image.assert_called_with('test_image:latest')
5268
assert isinstance(image, Image)
5369
assert image.id == FAKE_IMAGE_ID
5470

5571
def test_pull_multiple(self):
5672
client = make_fake_client()
57-
images = client.images.pull('test_image')
73+
images = client.images.pull('test_image', all_tags=True)
5874
client.api.pull.assert_called_with(
59-
'test_image', tag=None, stream=True
75+
'test_image', tag='latest', all_tags=True, stream=True
6076
)
6177
client.api.images.assert_called_with(
6278
all=False, name='test_image', filters=None

0 commit comments

Comments
 (0)