title: How do I limit the memory resource of a group of docker containers?
body: <p>I understand that I can use <code>--memory</code> and <code>--memory-swap</code> to limit memory resource per container. But, how do I limit memory resource per a group of containers?</p>
  
  <p>My system has 8GB RAM memory and consists of 2 docker containers. I want to set an 8 GB limit on both containers. I do not want to set a 4GB memory resource limit for each container as </p>
  
  <ol>
  <li>A container may use more than 4GB memory.</li>
  <li>Both containers won't use 4GB memory at the same time, so it would make sense to give the unused memory of container A to container B.</li>
  </ol>
  
  <p><strong>Things I have tried</strong></p>
  
  <ol>
  <li><p>The default parent cgroup for docker containers is "docker". "docker" is the parent cgroup of my containers. I tried setting the limit of the parent cgroup</p>
  
  <p>echo 8000000000 > /sys/fs/cgroup/memory/docker/memory.limit_in_bytes</p></li>
  <li><p>I check memory usage of the parent cgroup "docker" but it is  0, not equivalent to the sum of memory usage of its children.</p>
  
  <p>cat /sys/fs/cgroup/memory/docker/memory.usage_in_bytes # returns 0</p></li>
  </ol>
  
  <p>I appreciate your help.</p>