// Docker Resource Constraints
Generate docker run -m and docker-compose memory flags instantly. Covers --memory, --memory-swap, --memory-reservation, and OOM kill settings.
--memory vs --memory-reservation — what is the difference?
--memory (or -m) is a hard limit — the container cannot use more than this amount. If it tries to allocate more, the OOM killer terminates it. --memory-reservation is a soft limit — Docker will try to reclaim memory from the container when the host is under memory pressure, but the container can still exceed this value up to the hard limit. Use reservation for workloads that can tolerate some memory being reclaimed, and hard limit to absolutely cap a container's footprint.
--memory-swap explained
--memory-swap is the total memory + swap available to the container, not just the swap amount. If --memory=512m and --memory-swap=1g, the container can use 512MB RAM + 512MB swap. Set --memory-swap equal to --memory to disable swap entirely. Set to -1 for unlimited swap. If you set --memory without --memory-swap, Docker defaults to 2x memory for swap. On hosts without a swap partition, this flag has no effect.
What happens when a container exceeds its memory limit?
When a container's memory usage exceeds the hard limit, the Linux OOM (Out of Memory) killer terminates a process inside the container. This usually kills the main process, causing the container to exit. Docker will restart it if you have a restart policy (--restart always or --restart on-failure). You can disable OOM killing with --oom-kill-disable — but this can cause the host to hang if the container grows unbounded. Always combine --oom-kill-disable with a hard memory limit.