Skip to content

Commit 427e3a6

Browse files
committed
Moved mem_limit and memswap_limit to host_config for API version >= 1.19
1 parent e5485fb commit 427e3a6

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

docker/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,11 @@ def copy(self, container, resource):
464464

465465
def create_container(self, image, command=None, hostname=None, user=None,
466466
detach=False, stdin_open=False, tty=False,
467-
mem_limit=0, ports=None, environment=None, dns=None,
468-
volumes=None, volumes_from=None,
467+
mem_limit=None, ports=None, environment=None,
468+
dns=None, volumes=None, volumes_from=None,
469469
network_disabled=False, name=None, entrypoint=None,
470470
cpu_shares=None, working_dir=None, domainname=None,
471-
memswap_limit=0, cpuset=None, host_config=None,
471+
memswap_limit=None, cpuset=None, host_config=None,
472472
mac_address=None, labels=None, volume_driver=None):
473473

474474
if isinstance(volumes, six.string_types):

docker/utils/utils.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,21 @@ def create_host_config(
378378
dns=None, dns_search=None, volumes_from=None, network_mode=None,
379379
restart_policy=None, cap_add=None, cap_drop=None, devices=None,
380380
extra_hosts=None, read_only=None, pid_mode=None, ipc_mode=None,
381-
security_opt=None, ulimits=None, log_config=None
381+
security_opt=None, ulimits=None, log_config=None, mem_limit=None,
382+
memswap_limit=None
382383
):
383384
host_config = {}
384385

386+
if mem_limit is not None:
387+
if isinstance(mem_limit, six.string_types):
388+
mem_limit = parse_bytes(mem_limit)
389+
host_config['Memory'] = mem_limit
390+
391+
if memswap_limit is not None:
392+
if isinstance(memswap_limit, six.string_types):
393+
memswap_limit = parse_bytes(memswap_limit)
394+
host_config['MemorySwap'] = memswap_limit
395+
385396
if pid_mode not in (None, 'host'):
386397
raise errors.DockerException(
387398
'Invalid value for pid param: {0}'.format(pid_mode)
@@ -498,10 +509,10 @@ def create_host_config(
498509

499510
def create_container_config(
500511
version, image, command, hostname=None, user=None, detach=False,
501-
stdin_open=False, tty=False, mem_limit=0, ports=None, environment=None,
512+
stdin_open=False, tty=False, mem_limit=None, ports=None, environment=None,
502513
dns=None, volumes=None, volumes_from=None, network_disabled=False,
503514
entrypoint=None, cpu_shares=None, working_dir=None, domainname=None,
504-
memswap_limit=0, cpuset=None, host_config=None, mac_address=None,
515+
memswap_limit=None, cpuset=None, host_config=None, mac_address=None,
505516
labels=None, volume_driver=None
506517
):
507518
if isinstance(command, six.string_types):
@@ -517,10 +528,24 @@ def create_container_config(
517528
'labels were only introduced in API version 1.18'
518529
)
519530

520-
if volume_driver is not None and compare_version('1.19', version) < 0:
521-
raise errors.InvalidVersion(
522-
'Volume drivers were only introduced in API version 1.19'
523-
)
531+
if compare_version('1.19', version) < 0:
532+
if volume_driver is not None:
533+
raise errors.InvalidVersion(
534+
'Volume drivers were only introduced in API version 1.19'
535+
)
536+
mem_limit = mem_limit if mem_limit is not None else 0
537+
memswap_limit = memswap_limit if memswap_limit is not None else 0
538+
else:
539+
if mem_limit is not None:
540+
raise errors.InvalidVersion(
541+
'mem_limit has been moved to host_config in API version 1.19'
542+
)
543+
544+
if memswap_limit is not None:
545+
raise errors.InvalidVersion(
546+
'memswap_limit has been moved to host_config in API '
547+
'version 1.19'
548+
)
524549

525550
if isinstance(labels, list):
526551
labels = dict((lbl, six.text_type('')) for lbl in labels)

0 commit comments

Comments
 (0)