diff --git a/docker/utils/utils.py b/docker/utils/utils.py index 136466c8..ee1d1b0e 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -616,7 +616,7 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None, mem_limit=None, memswap_limit=None, mem_swappiness=None, cgroup_parent=None, group_add=None, cpu_quota=None, cpu_period=None, oom_kill_disable=False, shm_size=None, - version=None, tmpfs=None): + version=None, tmpfs=None, oom_score_adj=None): host_config = {} @@ -666,6 +666,15 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None, host_config['OomKillDisable'] = oom_kill_disable + if oom_score_adj: + if version_lt(version, '1.22'): + raise host_config_version_error('oom_score_adj', '1.22') + if not isinstance(oom_score_adj, int): + raise host_config_type_error( + 'oom_score_adj', oom_score_adj, 'int' + ) + host_config['OomScoreAdj'] = oom_score_adj + if publish_all_ports: host_config['PublishAllPorts'] = publish_all_ports diff --git a/docs/hostconfig.md b/docs/hostconfig.md index 19cd4ca2..cd96433e 100644 --- a/docs/hostconfig.md +++ b/docs/hostconfig.md @@ -73,6 +73,8 @@ for example: for more information. * lxc_conf (dict): LXC config * oom_kill_disable (bool): Whether to disable OOM killer +* oom_score_adj (int): An integer value containing the score given to the + container in order to tune OOM killer preferences * publish_all_ports (bool): Whether to publish all ports to the host * links (dict or list of tuples): either as a dictionary mapping name to alias or as a list of `(name, alias)` tuples diff --git a/tests/unit/utils_test.py b/tests/unit/utils_test.py index bd794598..eb952b2f 100644 --- a/tests/unit/utils_test.py +++ b/tests/unit/utils_test.py @@ -87,6 +87,16 @@ class HostConfigTest(base.BaseTestCase): InvalidVersion, lambda: create_host_config(version='1.18.3', oom_kill_disable=True)) + def test_create_host_config_with_oom_score_adj(self): + config = create_host_config(version='1.22', oom_score_adj=100) + self.assertEqual(config.get('OomScoreAdj'), 100) + self.assertRaises( + InvalidVersion, lambda: create_host_config(version='1.21', + oom_score_adj=100)) + self.assertRaises( + TypeError, lambda: create_host_config(version='1.22', + oom_score_adj='100')) + def test_create_endpoint_config_with_aliases(self): config = create_endpoint_config(version='1.22', aliases=['foo', 'bar']) assert config == {'Aliases': ['foo', 'bar']}