mirror of https://github.com/docker/docker-py.git
Merge pull request #834 from lots0logs/814-host-config-mem-swap
Add support for MemorySwappiness to client.create_host_config()
This commit is contained in:
commit
4e441b2a6b
|
|
@ -469,16 +469,17 @@ def parse_bytes(s):
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
def create_host_config(
|
def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
|
||||||
binds=None, port_bindings=None, lxc_conf=None,
|
publish_all_ports=False, links=None, privileged=False,
|
||||||
publish_all_ports=False, links=None, privileged=False,
|
dns=None, dns_search=None, volumes_from=None,
|
||||||
dns=None, dns_search=None, volumes_from=None, network_mode=None,
|
network_mode=None, restart_policy=None, cap_add=None,
|
||||||
restart_policy=None, cap_add=None, cap_drop=None, devices=None,
|
cap_drop=None, devices=None, extra_hosts=None,
|
||||||
extra_hosts=None, read_only=None, pid_mode=None, ipc_mode=None,
|
read_only=None, pid_mode=None, ipc_mode=None,
|
||||||
security_opt=None, ulimits=None, log_config=None, mem_limit=None,
|
security_opt=None, ulimits=None, log_config=None,
|
||||||
memswap_limit=None, cgroup_parent=None, group_add=None, cpu_quota=None,
|
mem_limit=None, memswap_limit=None, mem_swappiness=None,
|
||||||
cpu_period=None, version=None
|
cgroup_parent=None, group_add=None, cpu_quota=None,
|
||||||
):
|
cpu_period=None, version=None):
|
||||||
|
|
||||||
host_config = {}
|
host_config = {}
|
||||||
|
|
||||||
if not version:
|
if not version:
|
||||||
|
|
@ -491,6 +492,7 @@ def create_host_config(
|
||||||
if mem_limit is not None:
|
if mem_limit is not None:
|
||||||
if isinstance(mem_limit, six.string_types):
|
if isinstance(mem_limit, six.string_types):
|
||||||
mem_limit = parse_bytes(mem_limit)
|
mem_limit = parse_bytes(mem_limit)
|
||||||
|
|
||||||
host_config['Memory'] = mem_limit
|
host_config['Memory'] = mem_limit
|
||||||
|
|
||||||
if memswap_limit is not None:
|
if memswap_limit is not None:
|
||||||
|
|
@ -498,6 +500,18 @@ def create_host_config(
|
||||||
memswap_limit = parse_bytes(memswap_limit)
|
memswap_limit = parse_bytes(memswap_limit)
|
||||||
host_config['MemorySwap'] = memswap_limit
|
host_config['MemorySwap'] = memswap_limit
|
||||||
|
|
||||||
|
if mem_swappiness is not None:
|
||||||
|
if version_lt(version, '1.20'):
|
||||||
|
raise errors.InvalidVersion(
|
||||||
|
'mem_swappiness param not supported for API version < 1.20'
|
||||||
|
)
|
||||||
|
if not isinstance(mem_swappiness, int):
|
||||||
|
raise TypeError(
|
||||||
|
'Invalid type for mem_swappiness param: expected int but'
|
||||||
|
' found {0}'.format(type(mem_swappiness))
|
||||||
|
)
|
||||||
|
host_config['MemorySwappiness'] = mem_swappiness
|
||||||
|
|
||||||
if pid_mode not in (None, 'host'):
|
if pid_mode not in (None, 'host'):
|
||||||
raise errors.DockerException(
|
raise errors.DockerException(
|
||||||
'Invalid value for pid param: {0}'.format(pid_mode)
|
'Invalid value for pid param: {0}'.format(pid_mode)
|
||||||
|
|
|
||||||
|
|
@ -34,10 +34,11 @@ Docker bridge, 'none': no networking for this container, 'container:[name|id]':
|
||||||
reuses another container network stack), 'host': use the host network stack
|
reuses another container network stack), 'host': use the host network stack
|
||||||
inside the container.
|
inside the container.
|
||||||
|
|
||||||
`restart_policy` is available since v1.2.0 and sets the RestartPolicy for how a
|
`restart_policy` is available since v1.2.0 and sets the container's *RestartPolicy*
|
||||||
container should or should not be restarted on exit. By default the policy is
|
which defines the conditions under which a container should be restarted upon exit.
|
||||||
set to no meaning do not restart the container when it exits. The user may
|
If no *RestartPolicy* is defined, the container will not be restarted when it exits.
|
||||||
specify the restart policy as a dictionary for example:
|
The *RestartPolicy* is specified as a dict. For example, if the container
|
||||||
|
should always be restarted:
|
||||||
```python
|
```python
|
||||||
{
|
{
|
||||||
"MaximumRetryCount": 0,
|
"MaximumRetryCount": 0,
|
||||||
|
|
@ -45,8 +46,8 @@ specify the restart policy as a dictionary for example:
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
For always restarting the container on exit or can specify to restart the
|
It is possible to restart the container only on failure as well as limit the number
|
||||||
container to restart on failure and can limit number of restarts. For example:
|
of restarts. For example:
|
||||||
```python
|
```python
|
||||||
{
|
{
|
||||||
"MaximumRetryCount": 5,
|
"MaximumRetryCount": 5,
|
||||||
|
|
@ -95,10 +96,12 @@ for example:
|
||||||
of ulimits to be set in the container.
|
of ulimits to be set in the container.
|
||||||
* log_config (`docker.utils.LogConfig` or dict): Logging configuration to
|
* log_config (`docker.utils.LogConfig` or dict): Logging configuration to
|
||||||
container
|
container
|
||||||
* mem_limit (str or num): Maximum amount of memory container is allowed to
|
* mem_limit (str or int): Maximum amount of memory container is allowed to
|
||||||
consume. (e.g. `'1g'`)
|
consume. (e.g. `'1G'`)
|
||||||
* memswap_limit (str or num): Maximum amount of memory + swap a container is
|
* memswap_limit (str or int): Maximum amount of memory + swap a container is
|
||||||
allowed to consume.
|
allowed to consume.
|
||||||
|
* mem_swappiness (int): Tune a container's memory swappiness behavior.
|
||||||
|
Accepts number between 0 and 100.
|
||||||
* group_add (list): List of additional group names and/or IDs that the
|
* group_add (list): List of additional group names and/or IDs that the
|
||||||
container process will run as.
|
container process will run as.
|
||||||
* devices (list): A list of devices to add to the container specified as dicts
|
* devices (list): A list of devices to add to the container specified as dicts
|
||||||
|
|
|
||||||
|
|
@ -335,6 +335,38 @@ class CreateContainerTest(api_test.BaseTestCase):
|
||||||
self.assertEqual(container_log_config['Type'], "json-file")
|
self.assertEqual(container_log_config['Type'], "json-file")
|
||||||
self.assertEqual(container_log_config['Config'], {})
|
self.assertEqual(container_log_config['Config'], {})
|
||||||
|
|
||||||
|
def test_create_with_memory_constraints_with_str(self):
|
||||||
|
ctnr = self.client.create_container(
|
||||||
|
BUSYBOX, 'true',
|
||||||
|
host_config=self.client.create_host_config(
|
||||||
|
memswap_limit='1G',
|
||||||
|
mem_limit='700M'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertIn('Id', ctnr)
|
||||||
|
self.tmp_containers.append(ctnr['Id'])
|
||||||
|
self.client.start(ctnr)
|
||||||
|
inspect = self.client.inspect_container(ctnr)
|
||||||
|
|
||||||
|
self.assertIn('HostConfig', inspect)
|
||||||
|
host_config = inspect['HostConfig']
|
||||||
|
for limit in ['Memory', 'MemorySwap']:
|
||||||
|
self.assertIn(limit, host_config)
|
||||||
|
|
||||||
|
def test_create_with_memory_constraints_with_int(self):
|
||||||
|
ctnr = self.client.create_container(
|
||||||
|
BUSYBOX, 'true',
|
||||||
|
host_config=self.client.create_host_config(mem_swappiness=40)
|
||||||
|
)
|
||||||
|
self.assertIn('Id', ctnr)
|
||||||
|
self.tmp_containers.append(ctnr['Id'])
|
||||||
|
self.client.start(ctnr)
|
||||||
|
inspect = self.client.inspect_container(ctnr)
|
||||||
|
|
||||||
|
self.assertIn('HostConfig', inspect)
|
||||||
|
host_config = inspect['HostConfig']
|
||||||
|
self.assertIn('MemorySwappiness', host_config)
|
||||||
|
|
||||||
|
|
||||||
class VolumeBindTest(api_test.BaseTestCase):
|
class VolumeBindTest(api_test.BaseTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue