From 686d8e9536d89ac9c0259d9598fd03b65a9409e2 Mon Sep 17 00:00:00 2001 From: Thomas Schaaf Date: Mon, 9 Jan 2017 22:41:14 +0100 Subject: [PATCH] Implement cachefrom Signed-off-by: Thomas Schaaf --- docker/api/build.py | 11 ++++++++++- tests/integration/api_build_test.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/docker/api/build.py b/docker/api/build.py index eb01bce3..c009f1a2 100644 --- a/docker/api/build.py +++ b/docker/api/build.py @@ -18,7 +18,7 @@ class BuildApiMixin(object): custom_context=False, encoding=None, pull=False, forcerm=False, dockerfile=None, container_limits=None, decode=False, buildargs=None, gzip=False, shmsize=None, - labels=None): + labels=None, cachefrom=None): """ Similar to the ``docker build`` command. Either ``path`` or ``fileobj`` needs to be set. ``path`` can be a local path (to a directory @@ -92,6 +92,7 @@ class BuildApiMixin(object): shmsize (int): Size of `/dev/shm` in bytes. The size must be greater than 0. If omitted the system uses 64MB. labels (dict): A dictionary of labels to set on the image. + cachefrom (list): A list of images used for build cache resolution. Returns: A generator for the build output. @@ -188,6 +189,14 @@ class BuildApiMixin(object): 'labels was only introduced in API version 1.23' ) + if cachefrom: + if utils.version_gte(self._version, '1.25'): + params.update({'cachefrom': json.dumps(cachefrom)}) + else: + raise errors.InvalidVersion( + 'cachefrom was only introduced in API version 1.25' + ) + if context is not None: headers = {'Content-Type': 'application/tar'} if encoding: diff --git a/tests/integration/api_build_test.py b/tests/integration/api_build_test.py index 3dac0e93..e7479bfb 100644 --- a/tests/integration/api_build_test.py +++ b/tests/integration/api_build_test.py @@ -153,6 +153,24 @@ class BuildTest(BaseAPIIntegrationTest): info = self.client.inspect_image('labels') self.assertEqual(info['Config']['Labels'], labels) + @requires_api_version('1.25') + def test_build_cachefrom(self): + script = io.BytesIO('\n'.join([ + 'FROM scratch', + ]).encode('ascii')) + + cachefrom = ['build1'] + + stream = self.client.build( + fileobj=script, tag='cachefrom', cachefrom=cachefrom + ) + self.tmp_imgs.append('cachefrom') + for chunk in stream: + pass + + info = self.client.inspect_image('cachefrom') + self.assertEqual(info['Config']['CacheFrom'], cachefrom) + def test_build_stderr_data(self): control_chars = ['\x1b[91m', '\x1b[0m'] snippet = 'Ancient Temple (Mystic Oriental Dream ~ Ancient Temple)'