Skip to content

Commit b40ca4b

Browse files
committed
feat: support cpuperiod and cpuquota build arguments (Docker v1.19+)
Add support for the new container limits introduced in Docker v1.19 for restricting cpu usage during build. See https://docs.docker.com/engine/api/version-history/#v119-api-changes Signed-off-by: Lucy Linder <[email protected]>
1 parent b6464db commit b40ca4b

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

docker/api/build.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
8383
- cpushares (int): CPU shares (relative weight)
8484
- cpusetcpus (str): CPUs in which to allow execution, e.g.,
8585
``"0-3"``, ``"0,1"``
86+
- cpuperiod (int): (Docker API v1.19+) The length of a CPU
87+
period in microseconds
88+
- cpuquota (int): (Docker API v1.19+) Microseconds of CPU
89+
time that the container can get in a CPU period
8690
decode (bool): If set to ``True``, the returned stream will be
8791
decoded into dicts on the fly. Default ``False``
8892
shmsize (int): Size of `/dev/shm` in bytes. The size must be
@@ -135,6 +139,13 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
135139
raise errors.DockerException(
136140
f"invalid tag '{tag}': invalid reference format"
137141
)
142+
introduced_in = constants.CONTAINER_LIMITS_KEYS[key]
143+
if introduced_in is not None:
144+
if utils.version_lt(self._version, introduced_in):
145+
raise errors.InvalidVersion(
146+
f"container limit '{key}' was only introduced"
147+
f" in API version {introduced_in}"
148+
)
138149
if custom_context:
139150
if not fileobj:
140151
raise TypeError("You must specify fileobj with custom_context")

docker/constants.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
MINIMUM_DOCKER_API_VERSION = '1.24'
77
DEFAULT_TIMEOUT_SECONDS = 60
88
STREAM_HEADER_SIZE_BYTES = 8
9-
CONTAINER_LIMITS_KEYS = [
10-
'memory', 'memswap', 'cpushares', 'cpusetcpus'
11-
]
9+
CONTAINER_LIMITS_KEYS = {
10+
'memory': None,
11+
'memswap': None,
12+
'cpushares': None,
13+
'cpusetcpus': None,
14+
'cpuperiod': "1.19",
15+
'cpuquota': "1.19",
16+
}
1217

1318
DEFAULT_HTTP_HOST = "127.0.0.1"
1419
DEFAULT_UNIX_SOCKET = "http+unix:///var/run/docker.sock"

docker/models/images.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ def build(self, **kwargs):
259259
- cpushares (int): CPU shares (relative weight)
260260
- cpusetcpus (str): CPUs in which to allow execution, e.g.,
261261
``"0-3"``, ``"0,1"``
262+
- cpuperiod (int): (Docker API v1.19+) The length of a CPU
263+
period in microseconds
264+
- cpuquota (int): (Docker API v1.19+) Microseconds of CPU
265+
time that the container can get in a CPU period
262266
shmsize (int): Size of `/dev/shm` in bytes. The size must be
263267
greater than 0. If omitted the system uses 64MB
264268
labels (dict): A dictionary of labels to set on the image

tests/unit/api_build_test.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from docker import auth, errors
99
from docker.api.build import process_dockerfile
1010

11-
from ..helpers import make_tree
11+
from ..helpers import make_tree, requires_api_version
1212
from .api_test import BaseAPIClientTest, fake_request, url_prefix
1313

1414

@@ -133,6 +133,16 @@ def test_build_container_with_container_limits(self):
133133
},
134134
)
135135

136+
@requires_api_version('1.19')
137+
def test_build_container_with_container_limits_cpu(self):
138+
self.client.build(
139+
".",
140+
container_limits={
141+
"cpuquota": 50000,
142+
"cpuperiod": 100000,
143+
},
144+
)
145+
136146
def test_build_container_invalid_container_limits(self):
137147
with pytest.raises(docker.errors.DockerException):
138148
self.client.build(".", container_limits={"foo": "bar"})

0 commit comments

Comments
 (0)