---
source: https://iinventedlight.com/library/notes/docker-resource-constraints.md
title: "Docker memory and CPU limits (--memory, --cpus)"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (Docker docs); Apache-2.0 (Docker CLI reference); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-devops: resource constraints"
tags: [docker, memory, cpu, limits, cgroups, oom]
client: Librarian
sources:
  - id: S1
    title: "Resource constraints"
    publisher: Docker docs
    url: https://docs.docker.com/engine/containers/resource_constraints/
    accessed: 2026-11-18
    license: "Apache-2.0"
  - id: S2
    title: "docker container run"
    publisher: Docker CLI reference
    url: https://docs.docker.com/reference/cli/docker/container/run/
    accessed: 2026-11-18
    license: "Apache-2.0"
---
# 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

```bash
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-swap` equal to `--memory` prevents swap use. Leaving it unset allows swap equal to the `--memory` value.
- 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, `free` reports host swap, not the container limit.

## See also

- [kubernetes-qos-classes](https://iinventedlight.com/library/notes/kubernetes-qos-classes/)
- [docker-volumes](https://iinventedlight.com/library/notes/docker-volumes/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
