Skip to content

Commit 7af7e1b

Browse files
authored
Merge pull request #1612 from lawliet89/build-stage
Add `target` argument to image building
2 parents 7880c5a + e4093ab commit 7af7e1b

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

docker/api/build.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ 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):
21+
labels=None, cache_from=None, target=None):
2222
"""
2323
Similar to the ``docker build`` command. Either ``path`` or ``fileobj``
2424
needs to be set. ``path`` can be a local path (to a directory
@@ -94,6 +94,8 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
9494
labels (dict): A dictionary of labels to set on the image.
9595
cache_from (list): A list of images used for build cache
9696
resolution.
97+
target (str): Name of the build-stage to build in a multi-stage
98+
Dockerfile.
9799
98100
Returns:
99101
A generator for the build output.
@@ -198,6 +200,14 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
198200
'cache_from was only introduced in API version 1.25'
199201
)
200202

203+
if target:
204+
if utils.version_gte(self._version, '1.29'):
205+
params.update({'target': target})
206+
else:
207+
raise errors.InvalidVersion(
208+
'target was only introduced in API version 1.29'
209+
)
210+
201211
if context is not None:
202212
headers = {'Content-Type': 'application/tar'}
203213
if encoding:

docker/models/images.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ def build(self, **kwargs):
151151
decoded into dicts on the fly. Default ``False``.
152152
cache_from (list): A list of images used for build cache
153153
resolution.
154+
target (str): Name of the build-stage to build in a multi-stage
155+
Dockerfile.
154156
155157
Returns:
156158
(:py:class:`Image`): The built image.

tests/integration/api_build_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,28 @@ def test_build_with_cache_from(self):
189189
counter += 1
190190
assert counter == 0
191191

192+
@requires_api_version('1.29')
193+
def test_build_container_with_target(self):
194+
script = io.BytesIO('\n'.join([
195+
'FROM busybox as first',
196+
'RUN mkdir -p /tmp/test',
197+
'RUN touch /tmp/silence.tar.gz',
198+
'FROM alpine:latest',
199+
'WORKDIR /root/'
200+
'COPY --from=first /tmp/silence.tar.gz .',
201+
'ONBUILD RUN echo "This should not be in the final image"'
202+
]).encode('ascii'))
203+
204+
stream = self.client.build(
205+
fileobj=script, target='first', tag='build1'
206+
)
207+
self.tmp_imgs.append('build1')
208+
for chunk in stream:
209+
pass
210+
211+
info = self.client.inspect_image('build1')
212+
self.assertEqual(info['Config']['OnBuild'], [])
213+
192214
def test_build_stderr_data(self):
193215
control_chars = ['\x1b[91m', '\x1b[0m']
194216
snippet = 'Ancient Temple (Mystic Oriental Dream ~ Ancient Temple)'

0 commit comments

Comments
 (0)