Skip to content

Commit a6065df

Browse files
gferonshin-
authored andcommitted
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 <[email protected]>
1 parent d5c4ce2 commit a6065df

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

docker/api/build.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
1818
custom_context=False, encoding=None, pull=False,
1919
forcerm=False, dockerfile=None, container_limits=None,
2020
decode=False, buildargs=None, gzip=False, shmsize=None,
21-
labels=None, cache_from=None, target=None, network_mode=None):
21+
labels=None, cache_from=None, target=None, network_mode=None,
22+
squash=None):
2223
"""
2324
Similar to the ``docker build`` command. Either ``path`` or ``fileobj``
2425
needs to be set. ``path`` can be a local path (to a directory
@@ -98,6 +99,8 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
9899
Dockerfile
99100
network_mode (str): networking mode for the run commands during
100101
build
102+
squash (bool): Squash the resulting images layers into a
103+
single layer.
101104
102105
Returns:
103106
A generator for the build output.
@@ -218,6 +221,14 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
218221
'network_mode was only introduced in API version 1.25'
219222
)
220223

224+
if squash:
225+
if utils.version_gte(self._version, '1.25'):
226+
params.update({'squash': squash})
227+
else:
228+
raise errors.InvalidVersion(
229+
'squash was only introduced in API version 1.25'
230+
)
231+
221232
if context is not None:
222233
headers = {'Content-Type': 'application/tar'}
223234
if encoding:

tests/integration/api_build_test.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import six
1010

1111
from .base import BaseAPIIntegrationTest
12-
from ..helpers import requires_api_version
12+
from ..helpers import requires_api_version, requires_experimental
1313

1414

1515
class BuildTest(BaseAPIIntegrationTest):
@@ -244,6 +244,32 @@ def test_build_with_network_mode(self):
244244
with pytest.raises(errors.NotFound):
245245
self.client.inspect_image('dockerpytest_nonebuild')
246246

247+
@requires_api_version('1.25')
248+
@requires_experimental
249+
def test_build_squash(self):
250+
script = io.BytesIO('\n'.join([
251+
'FROM busybox',
252+
'RUN echo blah > /file_1',
253+
'RUN echo blahblah > /file_2',
254+
'RUN echo blahblahblah > /file_3'
255+
]).encode('ascii'))
256+
257+
def build_squashed(squash):
258+
tag = 'squash' if squash else 'nosquash'
259+
stream = self.client.build(
260+
fileobj=script, tag=tag, squash=squash
261+
)
262+
self.tmp_imgs.append(tag)
263+
for chunk in stream:
264+
pass
265+
266+
return self.client.inspect_image(tag)
267+
268+
non_squashed = build_squashed(False)
269+
squashed = build_squashed(True)
270+
self.assertEqual(len(non_squashed['RootFS']['Layers']), 4)
271+
self.assertEqual(len(squashed['RootFS']['Layers']), 2)
272+
247273
def test_build_stderr_data(self):
248274
control_chars = ['\x1b[91m', '\x1b[0m']
249275
snippet = 'Ancient Temple (Mystic Oriental Dream ~ Ancient Temple)'

0 commit comments

Comments
 (0)