Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion docker/api/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
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):
labels=None, cache_from=None, target=None):
"""
Similar to the ``docker build`` command. Either ``path`` or ``fileobj``
needs to be set. ``path`` can be a local path (to a directory
Expand Down Expand Up @@ -94,6 +94,8 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
labels (dict): A dictionary of labels to set on the image.
cache_from (list): A list of images used for build cache
resolution.
target (str): Name of the build-stage to build in a multi-stage
Dockerfile.

Returns:
A generator for the build output.
Expand Down Expand Up @@ -198,6 +200,14 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
'cache_from was only introduced in API version 1.25'
)

if target:
if utils.version_gte(self._version, '1.29'):
params.update({'target': target})
else:
raise errors.InvalidVersion(
'target was only introduced in API version 1.29'
)

if context is not None:
headers = {'Content-Type': 'application/tar'}
if encoding:
Expand Down
2 changes: 2 additions & 0 deletions docker/models/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def build(self, **kwargs):
decoded into dicts on the fly. Default ``False``.
cache_from (list): A list of images used for build cache
resolution.
target (str): Name of the build-stage to build in a multi-stage
Dockerfile.

Returns:
(:py:class:`Image`): The built image.
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/api_build_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,28 @@ def test_build_with_cache_from(self):
counter += 1
assert counter == 0

@requires_api_version('1.29')
def test_build_container_with_target(self):
script = io.BytesIO('\n'.join([
'FROM busybox as first',
'RUN mkdir -p /tmp/test',
'RUN touch /tmp/silence.tar.gz',
'FROM alpine:latest',
'WORKDIR /root/'
'COPY --from=first /tmp/silence.tar.gz .',
'ONBUILD RUN echo "This should not be in the final image"'
]).encode('ascii'))

stream = self.client.build(
fileobj=script, target='first', tag='build1'
)
self.tmp_imgs.append('build1')
for chunk in stream:
pass

info = self.client.inspect_image('build1')
self.assertEqual(info['Config']['OnBuild'], [])

def test_build_stderr_data(self):
control_chars = ['\x1b[91m', '\x1b[0m']
snippet = 'Ancient Temple (Mystic Oriental Dream ~ Ancient Temple)'
Expand Down