هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
docker run -d --name api --memory 512m --memory-swap 512m --cpus 1.5 --pids-limit 256 registry.example.com/shop/api:1.4.2
docker stats --no-stream
docker inspect api --format '{{.HostConfig.Memory}} {{.HostConfig.NanoCpus}}'--memory-swap = the same value as --memory → no extra swap for this container.
Step by step
- The limits:
| Option | Means |
|---|---|
--memory 512m | Hard memory cap; above it the container's process is OOM-killed. |
--memory-reservation 384m | Soft target when the host is short on memory. |
--cpus 1.5 | At most 1.5 CPU cores (throttled, not killed). |
--cpu-shares | Relative weight when CPUs are busy. |
--pids-limit 256 | Max processes (stops fork bombs). |
- In Compose:
services:
api:
image: registry.example.com/shop/api:1.4.2
mem_limit: 512m
cpus: 1.5
pids_limit: 256- Change a running container (no restart):
docker update --memory 768m --memory-swap 768m --cpus 2 api- Watch usage:
docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.PIDs}}'
NAME CPU % MEM USAGE / LIMIT PIDS
api 38.2% 401MiB / 512MiB 23- When the limit is hit:
docker inspect api --format '{{.State.OOMKilled}} {{.State.ExitCode}} {{.RestartCount}}'
true 137 3OOMKilled true + exit 137 = memory limit reached. With --restart unless-stopped it comes back — but find the reason.
- Runtimes must know the limit: Java (
-XX:MaxRAMPercentage=75), Node (--max-old-space-size=384), .NET and Go (GOMEMLIMIT) — otherwise they size memory by the host and get killed.
Check that it worked
docker stats shows the limit in MEM USAGE / LIMIT, docker inspect shows the values, and a load test stays within them without slowing the other containers.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Container restarts with exit 137 | OOM kill at the memory limit. | Raise the limit from measured peaks, or fix the leak; set the runtime's heap. |
Your kernel does not support swap limit capabilities | cgroup swap accounting off. | Warning only; on cgroup v2 hosts it works — update the host. |
| App very slow under load | --cpus too low (throttling). | Raise --cpus; check docker stats CPU near the limit. |
fork: Resource temporarily unavailable | --pids-limit too low. | Raise it for apps with many threads. |
| Limits in Compose ignored | Old Compose v2 file format with deploy.resources (Swarm only). | Use mem_limit/cpus or Compose Spec deploy.resources with a current docker compose. |
| Host still runs out of memory | Containers without limits. | Limit every container (or set defaults with your orchestrator). |
Go further
- Command pages: docker, podman.
- Next how-tos: Run a container and publish a port, Limit CPU and memory of a service (systemd), Set resource requests and limits (Kubernetes).
- Learn it: Docker — Limits, Docker — Compose limits.
1A container exits with code 137 and OOMKilled: true. What happened?
It used more memory than its --memory limit and the kernel killed it.
2What happens when a container reaches its --cpus limit?
It is throttled (gets slower); it is not killed.