Docker memory and CPU limits (--memory, --cpus)
Answer: --memory (-m) sets the maximum amount of memory the container can use. --cpus specifies how much of the available CPU resources a container can use. By default, a container has no resource constraints and can use as much of a resource as the host's kernel scheduler allows.
Examples
docker run -m 512m --memory-swap 512m myapp
docker run -m 1g --memory-reservation 768m myapp
docker run --cpus 1.5 myapp
docker run --cpuset-cpus 0,1 --cpu-shares 512 myapp
docker run --pids-limit 200 myapp
docker stats
Memory options
| Option | Description | |---|---| | -m, --memory | maximum memory the container can use; minimum 6m | | --memory-swap | total memory plus swap; only has meaning if --memory is also set | | --memory-reservation | soft limit smaller than --memory, used on contention or low memory | | --memory-swappiness | 0 to 100; 0 turns off anonymous page swapping | | --oom-kill-disable | only use together with -m |
CPU options
| Option | Description | |---|---| | --cpus | how much of the available CPU resources a container can use | | --cpu-period / --cpu-quota | CFS period and quota; --cpus is a more convenient alternative | | --cpuset-cpus | limit specific CPUs or cores, such as 0-3 or 1,3 | | --cpu-shares | relative weight, default 1024; only enforced when CPU cycles are constrained |
Rules
- With
--memory="300m"and--memory-swap="1g", the container can use 300m of memory and 700m swap. - Setting
--memory-swapequal to--memoryprevents swap use. Leaving it unset allows swap equal to the--memoryvalue. - When the kernel runs out of memory it throws an OOME and kills processes; containers are more likely to be killed than the Docker daemon.
--cpus=".5"is equivalent to--cpu-period=100000 --cpu-quota=50000.- Inside the container,
freereports host swap, not the container limit.
See also
Claims
- id: C1 claim: Containers are unconstrained by default. source: S1 evidence: "By default, a container has no resource constraints"
- id: C2 claim: --memory-swap needs --memory. source: S1 evidence: "--memory-swap is a modifier flag that only has meaning if --memory is also set."
- id: C3 claim: --pids-limit tunes the process limit. source: S2 evidence: "Tune container pids limit (set -1 for unlimited)"
Sources
- [S1] Resource constraints. Docker docs. https://docs.docker.com/engine/containers/resource_constraints/ . License: Apache-2.0. Accessed 2026-11-18.
- [S2] docker container run. Docker CLI reference. https://docs.docker.com/reference/cli/docker/container/run/ . License: Apache-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Docker docs, Resource constraints; Docker CLI reference, docker container run. Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Docker docs, Resource constraints; Docker CLI reference, docker container run. Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc.
Cite this
iinventedlight. 2026. "Docker memory and CPU limits (--memory, --cpus)." iinventedlight Library, iil:note:xjp92mrd, version of 2026-11-18. https://iinventedlight.com/library/notes/docker-resource-constraints/. License: CC-BY-4.0.
@misc{iil_note_xjp92mrd,
author = {{iinventedlight}},
title = {{Docker memory and CPU limits (--memory, --cpus)}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/docker-resource-constraints/},
note = {Permanent ID iil:note:xjp92mrd. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- docker-resource-constraints.table.json (table; CC-BY-4.0)
- docker-resource-constraints.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/docker-resource-constraints.md.