title: Able to malloc more than docker-compose mem_limit
body: <p>I'm trying to limit my container so that it doesn't take up all the RAM on the host. From <a href="https://docs.docker.com/engine/admin/resource_constraints/#limit-a-containers-access-to-memory" rel="nofollow noreferrer">the Docker docs</a> I understand that <code>--memory</code> limits the RAM and <code>--memory-swap</code> limits (RAM+swap). From <a href="https://docs.docker.com/compose/compose-file/compose-file-v2/#cpu-and-other-resources" rel="nofollow noreferrer">the docker-compose docs</a> it looks like the terms for those are <code>mem_limit</code> and <code>memswap_limit</code>, so I've constructed the following docker-compose file:</p>
  
  <pre><code>&gt; cat docker-compose.yml
  version: "2"
  
  services:
      stress:
          image: progrium/stress
          command: '-m 1 --vm-bytes 15G --vm-hang 0 --timeout 10s'
  
          mem_limit: 1g
          memswap_limit: 2g
  </code></pre>
  
  <p>The <a href="https://hub.docker.com/r/progrium/stress/" rel="nofollow noreferrer">progrium/stress image</a> just runs <code>stress</code>, which in this case spawns a single thread which requests 15GB RAM and holds on to it for 10 seconds.</p>
  
  <p>I'd expect this to crash, since 15>2. (It does crash if I ask for more RAM than the host has.)</p>
  
  <p>The kernel has cgroups enabled, and <code>docker stats</code> shows that the limit is being recognised:</p>
  
  <pre><code>&gt; docker stats
  CONTAINER       CPU %     MEM USAGE / LIMIT     MEM %       NET I/O       BLOCK I/O       PIDS
  7624a9605c70    0.00%     1024MiB / 1GiB        99.99%      396B / 0B     172kB / 0B      2
  </code></pre>
  
  <p>So what's going on? How do I actually limit the container?</p>
  
  <p><strong>Update:</strong>
  Watching <code>free</code>, it looks like the RAM usage is effectively limited (only 1GB of RAM is used) but the swap is not: the container will gradually increase swap usage until it's eaten though all of the swap and <code>stress</code> crashes (it takes about 20secs to get through 5GB of swap on my machine).</p>
  
  <p><strong>Update 2:</strong>
  Setting <code>mem_swappiness: 0</code> causes an immediate crash when requesting more memory than <code>mem_limit</code>, regardless of <code>memswap_limit</code>.</p>