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>
This commit is contained in:
Gabriel Féron 2017-01-30 19:06:20 +01:00 committed by Joffrey F
parent d5c4ce203a
commit a6065df64d
2 changed files with 39 additions and 2 deletions

View File

@ -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:

View File

@ -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)'