How to limit memory usage in docker-compose?

deploy key only works in swarm mode and with docker-compose file version 3 and above.

In your case, use docker-compose file version 2 and define resource limits:

version: "2.2"

services:
  app:
    image: foo
    cpus: "0.5"
    mem_limit: 23m

See official docs here


Are you running the docker-compose in swarm mode ? If not Recommended to run 2.x version of compose file format.

3.X require docker-compose to be run in swarm mode for new set of resource directives to take effect.

Alternatives in 2.X are cpu_shares, cpu_quota, cpuset, mem_limit, memswap_limit, mem_swappiness