diff options
author | Gabriel Féron <feron.gabriel@gmail.com> | 2017-01-30 19:06:20 +0100 |
---|---|---|
committer | Joffrey F <f.joffrey@gmail.com> | 2017-08-17 13:38:40 -0700 |
commit | a6065df64d848a0fc1ce9f2638b7b2a33f407145 (patch) | |
tree | 0ce0786bb44a816e02ffab3b18a2eb8a998d57d6 | |
parent | d5c4ce203aa5839966a221079d1be44e572e92af (diff) | |
download | docker-py-a6065df64d848a0fc1ce9f2638b7b2a33f407145.tar.gz |
Add support for the `squash` flag when building
Also added a test that compares the number of layers in the default mode, and with the new flag
Signed-off-by: Gabriel Féron <feron.gabriel@gmail.com>
-rw-r--r-- | docker/api/build.py | 13 | ||||
-rw-r--r-- | tests/integration/api_build_test.py | 28 |
2 files changed, 39 insertions, 2 deletions
diff --git a/docker/api/build.py b/docker/api/build.py index 5d4e772..f9678a3 100644 --- a/docker/api/build.py +++ b/docker/api/build.py @@ -18,7 +18,8 @@ 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, cache_from=None, target=None, network_mode=None): + labels=None, cache_from=None, target=None, network_mode=None, + squash=None): """ Similar to the ``docker build`` command. Either ``path`` or ``fileobj`` needs to be set. ``path`` can be a local path (to a directory @@ -98,6 +99,8 @@ class BuildApiMixin(object): Dockerfile network_mode (str): networking mode for the run commands during build + squash (bool): Squash the resulting images layers into a + single layer. Returns: A generator for the build output. @@ -218,6 +221,14 @@ class BuildApiMixin(object): 'network_mode was only introduced in API version 1.25' ) + if squash: + if utils.version_gte(self._version, '1.25'): + params.update({'squash': squash}) + else: + raise errors.InvalidVersion( + 'squash 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 609964f..209c1f2 100644 --- a/tests/integration/api_build_test.py +++ b/tests/integration/api_build_test.py @@ -9,7 +9,7 @@ import pytest import six from .base import BaseAPIIntegrationTest -from ..helpers import requires_api_version +from ..helpers import requires_api_version, requires_experimental class BuildTest(BaseAPIIntegrationTest): @@ -244,6 +244,32 @@ class BuildTest(BaseAPIIntegrationTest): with pytest.raises(errors.NotFound): self.client.inspect_image('dockerpytest_nonebuild') + @requires_api_version('1.25') + @requires_experimental + def test_build_squash(self): + script = io.BytesIO('\n'.join([ + 'FROM busybox', + 'RUN echo blah > /file_1', + 'RUN echo blahblah > /file_2', + 'RUN echo blahblahblah > /file_3' + ]).encode('ascii')) + + def build_squashed(squash): + tag = 'squash' if squash else 'nosquash' + stream = self.client.build( + fileobj=script, tag=tag, squash=squash + ) + self.tmp_imgs.append(tag) + for chunk in stream: + pass + + return self.client.inspect_image(tag) + + non_squashed = build_squashed(False) + squashed = build_squashed(True) + self.assertEqual(len(non_squashed['RootFS']['Layers']), 4) + self.assertEqual(len(squashed['RootFS']['Layers']), 2) + def test_build_stderr_data(self): control_chars = ['\x1b[91m', '\x1b[0m'] snippet = 'Ancient Temple (Mystic Oriental Dream ~ Ancient Temple)' |