Skip to content

Commit

Permalink
feat: add cgroup to host config--according to api docs (closes#2340)
Browse files Browse the repository at this point in the history
  • Loading branch information
Khushiyant committed Mar 1, 2025
1 parent db7f8b8 commit c13794c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions docker/models/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ def run(self, image, command=None, stdout=True, stderr=False,
``["SYS_ADMIN", "MKNOD"]``.
cap_drop (list of str): Drop kernel capabilities.
cgroup_parent (str): Override the default parent cgroup.
cgroup (str): Cgroup to use for the container.
cgroupns (str): Override the default cgroup namespace mode for the
container. One of:
- ``private`` the container runs in its own private cgroup
Expand Down
6 changes: 5 additions & 1 deletion docker/types/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def __init__(self, version, binds=None, port_bindings=None,
nano_cpus=None, cpuset_mems=None, runtime=None, mounts=None,
cpu_rt_period=None, cpu_rt_runtime=None,
device_cgroup_rules=None, device_requests=None,
cgroupns=None):
cgroupns=None, cgroup=None):

if mem_limit is not None:
self['Memory'] = parse_bytes(mem_limit)
Expand Down Expand Up @@ -424,6 +424,10 @@ def __init__(self, version, binds=None, port_bindings=None,
if cgroup_parent is not None:
self['CgroupParent'] = cgroup_parent

if cgroup is not None:
if isinstance(cgroup, str):
self['Cgroup'] = cgroup

if ulimits is not None:
if not isinstance(ulimits, list):
raise host_config_type_error('ulimits', ulimits, 'list')
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/api_container_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,20 @@ def test_create_container_with_cgroup_parent(self):
assert 'CgroupParent' in data['HostConfig']
assert data['HostConfig']['CgroupParent'] == 'test'

def test_create_container_with_cgroup(self):
self.client.create_container(
'busybox', 'ls', host_config=self.client.create_host_config(
cgroup='host'
)
)

args = fake_request.call_args
assert args[0][1] == url_prefix + 'containers/create'
data = json.loads(args[1]['data'])
assert 'HostConfig' in data
assert 'Cgroup' in data['HostConfig']
assert data['HostConfig']['Cgroup'] == 'host'

def test_create_container_with_working_dir(self):
self.client.create_container('busybox', 'ls',
working_dir='/root')
Expand Down
1 change: 1 addition & 0 deletions tests/unit/models_containers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_create_container_args(self):
'cap_add': ['foo'],
'cap_drop': ['bar'],
'cgroup_parent': 'foobar',
'cgroup': 'host',
'cgroupns': 'host',
'cpu_period': 1,
'cpu_quota': 2,
Expand Down

0 comments on commit c13794c

Please sign in to comment.