متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 144Containers · Docker

How to limit CPU and memory of a container

One container with a memory leak or a busy loop can slow down every other container on the host — or make the kernel kill the database. Docker and Podman can cap each container's memory, CPU and number of processes. This how-to sets the limits with docker run, in Compose, and on a running container, watches usage, and explains what happens at the limit.

  • 5 الدروس
  • 2 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

144.1

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.

الأوامر في هذا الدرس

144.2

Step by step

  1. The limits:
OptionMeans
--memory 512mHard memory cap; above it the container's process is OOM-killed.
--memory-reservation 384mSoft target when the host is short on memory.
--cpus 1.5At most 1.5 CPU cores (throttled, not killed).
--cpu-sharesRelative weight when CPUs are busy.
--pids-limit 256Max processes (stops fork bombs).
  1. In Compose:
compose.yaml (part)
services:
  api:
    image: registry.example.com/shop/api:1.4.2
    mem_limit: 512m
    cpus: 1.5
    pids_limit: 256
  1. Change a running container (no restart):
docker update --memory 768m --memory-swap 768m --cpus 2 api
  1. 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
  1. When the limit is hit:
docker inspect api --format '{{.State.OOMKilled}} {{.State.ExitCode}} {{.RestartCount}}'
true 137 3

OOMKilled true + exit 137 = memory limit reached. With --restart unless-stopped it comes back — but find the reason.

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

الأوامر في هذا الدرس

144.3

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.

الأوامر في هذا الدرس

144.4

Common problems

ProblemCauseFix
Container restarts with exit 137OOM 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 capabilitiescgroup 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 ignoredOld 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 memoryContainers without limits.Limit every container (or set defaults with your orchestrator).

الأوامر في هذا الدرس

144.5

Go further

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.

كل الأوامر في هذا الفصل