Skip to content

Commit 2ef02df

Browse files
authored
Merge pull request #1168 from docker/aiden0z-master
Support pids_limit in HostConfig
2 parents 3419145 + 902c7a7 commit 2ef02df

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

docker/utils/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
623623
device_write_iops=None, oom_kill_disable=False,
624624
shm_size=None, sysctls=None, version=None, tmpfs=None,
625625
oom_score_adj=None, dns_opt=None, cpu_shares=None,
626-
cpuset_cpus=None, userns_mode=None):
626+
cpuset_cpus=None, userns_mode=None, pids_limit=None):
627627

628628
host_config = {}
629629

@@ -904,6 +904,13 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
904904
raise host_config_value_error("userns_mode", userns_mode)
905905
host_config['UsernsMode'] = userns_mode
906906

907+
if pids_limit:
908+
if not isinstance(pids_limit, int):
909+
raise host_config_type_error('pids_limit', pids_limit, 'int')
910+
if version_lt(version, '1.23'):
911+
raise host_config_version_error('pids_limit', '1.23')
912+
host_config["PidsLimit"] = pids_limit
913+
907914
return host_config
908915

909916

docs/hostconfig.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,12 @@ for example:
111111
CPU period.
112112
* cpu_shares (int): CPU shares (relative weight)
113113
* cpuset_cpus (str): CPUs in which to allow execution (0-3, 0,1)
114-
* blkio_weight: Block IO weight (relative weight), accepts a weight value between 10 and 1000.
114+
* blkio_weight: Block IO weight (relative weight), accepts a weight value
115+
between 10 and 1000.
115116
* blkio_weight_device: Block IO weight (relative device weight) in the form of:
116117
`[{"Path": "device_path", "Weight": weight}]`
117-
* device_read_bps: Limit read rate (bytes per second) from a device in the form of:
118-
`[{"Path": "device_path", "Rate": rate}]`
118+
* device_read_bps: Limit read rate (bytes per second) from a device in the
119+
form of: `[{"Path": "device_path", "Rate": rate}]`
119120
* device_write_bps: Limit write rate (bytes per second) from a device.
120121
* device_read_iops: Limit read rate (IO per second) from a device.
121122
* device_write_iops: Limit write rate (IO per second) from a device.
@@ -128,6 +129,7 @@ for example:
128129
* sysctls (dict): Kernel parameters to set in the container.
129130
* userns_mode (str): Sets the user namespace mode for the container when user
130131
namespace remapping option is enabled. Supported values are: `host`
132+
* pids_limit (int): Tune a container’s pids limit. Set -1 for unlimited.
131133

132134
**Returns** (dict) HostConfig dictionary
133135

tests/unit/utils_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ def test_create_host_config_with_kernel_memory(self):
185185
InvalidVersion, lambda: create_host_config(
186186
version='1.20', kernel_memory=67108864))
187187

188+
def test_create_host_config_with_pids_limit(self):
189+
config = create_host_config(version='1.23', pids_limit=1024)
190+
self.assertEqual(config.get('PidsLimit'), 1024)
191+
192+
with pytest.raises(InvalidVersion):
193+
create_host_config(version='1.22', pids_limit=1024)
194+
with pytest.raises(TypeError):
195+
create_host_config(version='1.22', pids_limit='1024')
196+
188197

189198
class UlimitTest(base.BaseTestCase):
190199
def test_create_host_config_dict_ulimit(self):

0 commit comments

Comments
 (0)