mirror of https://github.com/docker/docker-py.git
Merge pull request #209 from tarnfeld/feature/build-context
Added ability to specify the full build context
This commit is contained in:
commit
9bb292bcbd
|
@ -30,7 +30,8 @@ is hosted. `version` is the version of the API the client will use and
|
||||||
|
|
||||||
```python
|
```python
|
||||||
c.build(path=None, tag=None, quiet=False, fileobj=None, nocache=False,
|
c.build(path=None, tag=None, quiet=False, fileobj=None, nocache=False,
|
||||||
rm=False, stream=False)
|
rm=False, stream=False, timeout=None,
|
||||||
|
custom_context=False, encoding=None):
|
||||||
```
|
```
|
||||||
|
|
||||||
Similar to the `docker build` command. Either `path` or `fileobj` needs
|
Similar to the `docker build` command. Either `path` or `fileobj` needs
|
||||||
|
@ -38,6 +39,11 @@ to be set. `path` can be a local path (to a directory containing a
|
||||||
Dockerfile) or a remote URL. `fileobj` must be a readable file-like
|
Dockerfile) or a remote URL. `fileobj` must be a readable file-like
|
||||||
object to a Dockerfile.
|
object to a Dockerfile.
|
||||||
|
|
||||||
|
If you have a tar file for the docker build context (including a dockerfile)
|
||||||
|
already, pass a readable file-like object to `fileobj` and also pass
|
||||||
|
`custom_context=True`. If the stream is compressed also, set `encoding` to
|
||||||
|
the correct value (e.g `gzip`).
|
||||||
|
|
||||||
```python
|
```python
|
||||||
c.commit(container, repository=None, tag=None, message=None, author=None,
|
c.commit(container, repository=None, tag=None, message=None, author=None,
|
||||||
conf=None)
|
conf=None)
|
||||||
|
|
|
@ -317,14 +317,20 @@ class Client(requests.Session):
|
||||||
u, None, params=self._attach_params(params), stream=True))
|
u, None, params=self._attach_params(params), stream=True))
|
||||||
|
|
||||||
def build(self, path=None, tag=None, quiet=False, fileobj=None,
|
def build(self, path=None, tag=None, quiet=False, fileobj=None,
|
||||||
nocache=False, rm=False, stream=False, timeout=None):
|
nocache=False, rm=False, stream=False, timeout=None,
|
||||||
|
custom_context=False, encoding=None):
|
||||||
remote = context = headers = None
|
remote = context = headers = None
|
||||||
if path is None and fileobj is None:
|
if path is None and fileobj is None:
|
||||||
raise TypeError("Either path or fileobj needs to be provided.")
|
raise TypeError("Either path or fileobj needs to be provided.")
|
||||||
|
|
||||||
if fileobj is not None:
|
if custom_context:
|
||||||
|
if not fileobj:
|
||||||
|
raise TypeError("You must specify fileobj with custom_context")
|
||||||
|
context = fileobj
|
||||||
|
elif fileobj is not None:
|
||||||
context = utils.mkbuildcontext(fileobj)
|
context = utils.mkbuildcontext(fileobj)
|
||||||
elif path.startswith(('http://', 'https://', 'git://', 'github.com/')):
|
elif path.startswith(('http://', 'https://',
|
||||||
|
'git://', 'github.com/')):
|
||||||
remote = path
|
remote = path
|
||||||
else:
|
else:
|
||||||
context = utils.tar(path)
|
context = utils.tar(path)
|
||||||
|
@ -340,8 +346,11 @@ class Client(requests.Session):
|
||||||
'nocache': nocache,
|
'nocache': nocache,
|
||||||
'rm': rm
|
'rm': rm
|
||||||
}
|
}
|
||||||
|
|
||||||
if context is not None:
|
if context is not None:
|
||||||
headers = {'Content-Type': 'application/tar'}
|
headers = {'Content-Type': 'application/tar'}
|
||||||
|
if encoding:
|
||||||
|
headers['Content-Encoding'] = encoding
|
||||||
|
|
||||||
if utils.compare_version('1.9', self._version) >= 0:
|
if utils.compare_version('1.9', self._version) >= 0:
|
||||||
# If we don't have any auth data so far, try reloading the config
|
# If we don't have any auth data so far, try reloading the config
|
||||||
|
|
|
@ -20,6 +20,7 @@ import os
|
||||||
import signal
|
import signal
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
import gzip
|
||||||
|
|
||||||
import docker
|
import docker
|
||||||
import requests
|
import requests
|
||||||
|
@ -1240,6 +1241,41 @@ class DockerClientTest(unittest.TestCase):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.fail('Command should not raise exception: {0}'.format(e))
|
self.fail('Command should not raise exception: {0}'.format(e))
|
||||||
|
|
||||||
|
def test_build_container_custom_context(self):
|
||||||
|
script = io.BytesIO('\n'.join([
|
||||||
|
'FROM busybox',
|
||||||
|
'MAINTAINER docker-py',
|
||||||
|
'RUN mkdir -p /tmp/test',
|
||||||
|
'EXPOSE 8080',
|
||||||
|
'ADD https://dl.dropboxusercontent.com/u/20637798/silence.tar.gz'
|
||||||
|
' /tmp/silence.tar.gz'
|
||||||
|
]).encode('ascii'))
|
||||||
|
context = docker.utils.mkbuildcontext(script)
|
||||||
|
try:
|
||||||
|
self.client.build(fileobj=context, custom_context=True)
|
||||||
|
except Exception as e:
|
||||||
|
self.fail('Command should not raise exception: {0}'.format(e))
|
||||||
|
|
||||||
|
def test_build_container_custom_context_gzip(self):
|
||||||
|
script = io.BytesIO('\n'.join([
|
||||||
|
'FROM busybox',
|
||||||
|
'MAINTAINER docker-py',
|
||||||
|
'RUN mkdir -p /tmp/test',
|
||||||
|
'EXPOSE 8080',
|
||||||
|
'ADD https://dl.dropboxusercontent.com/u/20637798/silence.tar.gz'
|
||||||
|
' /tmp/silence.tar.gz'
|
||||||
|
]).encode('ascii'))
|
||||||
|
context = docker.utils.mkbuildcontext(script)
|
||||||
|
gz_context = gzip.GzipFile(fileobj=context)
|
||||||
|
try:
|
||||||
|
self.client.build(
|
||||||
|
fileobj=gz_context,
|
||||||
|
custom_context=True,
|
||||||
|
encoding="gzip"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
self.fail('Command should not raise exception: {0}'.format(e))
|
||||||
|
|
||||||
#######################
|
#######################
|
||||||
# PY SPECIFIC TESTS #
|
# PY SPECIFIC TESTS #
|
||||||
#######################
|
#######################
|
||||||
|
|
Loading…
Reference in New Issue