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
27 changes: 16 additions & 11 deletions docker/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,22 @@
import requests.exceptions
import six

from . import constants
from . import errors
from .auth import auth
from .unixconn import unixconn
from .ssladapter import ssladapter
from .utils import utils, check_resource
from . import errors
from .tls import TLSConfig


if not six.PY3:
import websocket

DEFAULT_DOCKER_API_VERSION = '1.18'
DEFAULT_TIMEOUT_SECONDS = 60
STREAM_HEADER_SIZE_BYTES = 8


class Client(requests.Session):
def __init__(self, base_url=None, version=None,
timeout=DEFAULT_TIMEOUT_SECONDS, tls=False):
timeout=constants.DEFAULT_TIMEOUT_SECONDS, tls=False):
super(Client, self).__init__()

if tls and not base_url.startswith('https://'):
Expand Down Expand Up @@ -70,7 +67,7 @@ def __init__(self, base_url=None, version=None,

# version detection needs to be after unix adapter mounting
if version is None:
self._version = DEFAULT_DOCKER_API_VERSION
self._version = constants.DEFAULT_DOCKER_API_VERSION
elif isinstance(version, six.string_types):
if version.lower() == 'auto':
self._version = self._retrieve_server_version()
Expand Down Expand Up @@ -218,7 +215,7 @@ def _multiplexed_buffer_helper(self, response):
if len(buf[walker:]) < 8:
break
_, length = struct.unpack_from('>BxxxL', buf[walker:])
start = walker + STREAM_HEADER_SIZE_BYTES
start = walker + constants.STREAM_HEADER_SIZE_BYTES
end = start + length
walker = end
yield buf[start:end]
Expand All @@ -236,7 +233,7 @@ def _multiplexed_response_stream_helper(self, response):
socket.settimeout(None)

while True:
header = response.raw.read(STREAM_HEADER_SIZE_BYTES)
header = response.raw.read(constants.STREAM_HEADER_SIZE_BYTES)
if not header:
break
_, length = struct.unpack('>BxxxL', header)
Expand Down Expand Up @@ -310,11 +307,18 @@ def attach_socket(self, container, params=None, ws=False):
def build(self, path=None, tag=None, quiet=False, fileobj=None,
nocache=False, rm=False, stream=False, timeout=None,
custom_context=False, encoding=None, pull=True,
forcerm=False, dockerfile=None):
forcerm=False, dockerfile=None, container_limits=None):
remote = context = headers = None
container_limits = container_limits or {}
if path is None and fileobj is None:
raise TypeError("Either path or fileobj needs to be provided.")

for key in container_limits.keys():
if key not in constants.CONTAINER_LIMITS_KEYS:
raise errors.DockerException(
'Invalid container_limits key {0}'.format(key)
)

if custom_context:
if not fileobj:
raise TypeError("You must specify fileobj with custom_context")
Expand Down Expand Up @@ -357,8 +361,9 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
'rm': rm,
'forcerm': forcerm,
'pull': pull,
'dockerfile': dockerfile
'dockerfile': dockerfile,
}
params.update(container_limits)

if context is not None:
headers = {'Content-Type': 'application/tar'}
Expand Down
6 changes: 6 additions & 0 deletions docker/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DEFAULT_DOCKER_API_VERSION = '1.18'
DEFAULT_TIMEOUT_SECONDS = 60
STREAM_HEADER_SIZE_BYTES = 8
CONTAINER_LIMITS_KEYS = [
'memory', 'memswap', 'cpushares', 'cpusetcpus'
]
6 changes: 6 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ correct value (e.g `gzip`).
* pull (bool): Downloads any updates to the FROM image in Dockerfiles
* forcerm (bool): Always remove intermediate containers, even after unsuccessful builds
* dockerfile (str): path within the build context to the Dockerfile
* container_limits (dict): A dictionary of limits applied to each container
created by the build process. Valid keys:
- memory (int): set memory limit for build
- memswap (int): Total memory (memory + swap), -1 to disable swap
- cpushares (int): CPU shares (relative weight)
- cpusetcpus (str): CPUs in which to allow exection, e.g., `"0-3"`, `"0,1"`

**Returns** (generator): A generator of the build output

Expand Down
Loading