Runtime constraints on CPU and memory with docker containers

910 views Asked by At

How can we change the memory and cpu limit for docker containers at runtime? I mean while the container is running I would like to change the the memory limit for example

Thanks in advance

1

There are 1 answers

1
christian On

You cannot change that inside the running container, you would have to do that on your host.

How you do that on the host depends on your host-os, on Linux I suggest to take a look a cgroups, thats how docker internally restricts containers.

On ubuntu you could use the cgroup manager cgm (tried it on ubuntu 15.04).

  1. create a new cgroup for cpu, move the process (e.g. 28433) to it and set a value
> # cgm create cpu dudecpu 
> # cgm movepid cpu dudecpu 28433 
> # cgm setvalue cpu dudecpu cpu.shares 512
  1. create a new cgroup for the memory move the process (e.g. 28433) to it and set a value
> cgm create memory dudemem
> cgm movepid memory dudemem 28433
> cgm setvalue memory dudemem memory.limit_in_bytes 1000000000
  1. check where your new cgroups are and look into these directories, you'll find all the properties of the cgroup there.
> find /sys/fs/cgroup/ -name "dude*"
> /sys/fs/cgroup/memory/user.slice/user-1000.slice/session-c3.scope/dudemem
> /sys/fs/cgroup/cpu,cpuacct/user.slice/user-1000.slice/session-c3.scope/dudecpu