Search code examples
dockercgroups

Docker multiple containers cgroup resource CPU MEM limitation


I'm using Ubuntu 21.10 and would like to restrict the system resources (CPU, MEM) for a subset of my docker containers - not for a particular container. How can I do that?


Solution

  • I figured out some solution. There is a docker param to us on a container start.

    First you need to create mygroup.slice in /etc/systemd/system/mygroup.slice and run systemctl daemon-reload.

    [Unit]
    Description=my cgroup for Docker
    Before=slices.target
    
    [Slice]
    MemoryAccounting=true
    MemoryHigh=7G
    MemoryMax=7.5G
    CPUAccounting=true
    CPUQuota=50%
    

    After that one can start a set of containers with the next argument.

    --cgroup-parent=mygroup.slice
    

    And finally run systemd-cgtop to check the resource allocation.