How to Limit Docker Container Memory and CPU Usage

How to Limit Docker Container Memory and CPU Usage

One runaway container can slow every other app on a VPS. A web app with a memory leak, a database doing a heavy import, or a build job eating every core will leave nothing for the rest of the stack. Setting docker container memory and cpu limits stops that. Each container gets a ceiling, and when it hits that ceiling, Docker throttles it or kills it instead of letting it take the whole host down.

This guide covers the flags for docker run, the matching settings for Compose, and how to check whether the limits actually hold. You need a host with Docker already installed and one container you can restart. Changing a limit on a running container is possible for some settings, but recreating the container is the clean way to apply a full set.

How Docker Container Memory and CPU Limits Work

Docker enforces limits through Linux control groups (cgroups). A memory limit caps how much RAM a container can use. A CPU limit caps how much processor time it can use, measured in fractions of a core. These are ceilings, not reservations: a container limited to 1 GB of RAM can still use less, and a container limited to 1 CPU can still burst up to that full core when it needs to.

That distinction matters when you size a stack. If you run six containers on a 4 GB VPS and give each one a 1 GB limit, the limits add up to more than the host has. That is fine as long as they do not all peak at once, but it is not a guarantee. When you size limits, start from what each app normally uses, not from the host total. A quick way to see normal use is to watch how to check VPS resource usage with htop, df, and free before you set anything.

Set a Docker Memory Limit with docker run

The main flag is --memory, often written as -m. It accepts values like 512m or 2g. The matching --memory-swap flag controls RAM plus swap combined. If you set memory without setting swap, Docker sets swap to twice the memory limit by default, which can hide a memory problem by letting the container grind away in swap instead of failing fast.

docker run -d --name webapp 
  --memory=1g 
  --memory-swap=1g 
  my-webapp:latest

Setting swap equal to memory means the container gets no swap at all. That is usually what you want on a small VPS, because swap on a slow disk makes a struggling app slower for everyone. If your host is already tight on RAM, read when swap on a VPS helps and how to add it before you rely on swap to cover a gap in RAM.

Two more flags help with specific cases. --memory-reservation sets a soft limit: the kernel starts reclaiming memory from the container when the host is under pressure, but the container can go past it when RAM is free. --oom-kill-disable stops the kernel from killing the container when it runs out of memory, but that flag can freeze the whole host instead, so leave it off unless you have a reason and a host you can afford to reboot.

Set a Docker CPU Limit with docker run

The simplest CPU flag is --cpus. It takes a decimal number of cores, so --cpus=0.5 gives the container half a core and --cpus=2 gives it two full cores. This is a hard ceiling measured over time: a container with 0.5 CPUs can use one core fully for half of each scheduling period, or spread the work across cores, but it cannot average more than half a core.

docker run -d --name worker 
  --cpus=1.5 
  --memory=512m 
  my-worker:latest

Older flags still show up in tutorials, and they behave differently. --cpu-shares sets a relative weight, not a ceiling: a container with twice the shares of another gets roughly twice the CPU only when both compete for it. On an idle host, a low-shares container can still use everything. --cpuset-cpus pins a container to specific cores, which helps for noisy workloads but wastes capacity when those cores sit idle. For most self-hosted stacks, --cpus is the flag to reach for first.

Flag What it does Use it when
--cpus Hard ceiling in cores You want a predictable cap
--cpu-shares Relative weight under contention You want priority, not a cap
--cpuset-cpus Pins container to specific cores You need to isolate a noisy job

Set Docker Compose Resource Limits

In Compose, the plain mem_limit and cpus keys apply on a single host without extra setup. This is the form to use for a normal self-hosted stack. The deploy.resources block is aimed at Swarm mode, so do not assume it will take effect on a plain docker compose up setup unless you test it and confirm with docker stats.

services:
  webapp:
    image: my-webapp:latest
    mem_limit: 1g
    cpus: 1.0
  worker:
    image: my-worker:latest
    mem_limit: 512m
    cpus: 0.5

After you change a Compose file, recreate the containers with docker compose up -d. Compose will rebuild the affected containers with the new limits. Verify the result instead of trusting the file: run docker inspect webapp and look for the memory and CPU values in the host config, or run docker stats and watch the live numbers. If you are still learning Compose syntax, the walkthrough in Docker Compose explained through your first file covers service structure before you add limits.

Check Limits and Fix Common Problems

The fastest check is docker stats, which shows live CPU percentage, memory use against the limit, and network and disk activity per container. For docker stats memory usage readings, watch the MEM USAGE / LIMIT column: a container sitting at 98% of its limit is about to be killed or throttled. For a single container, docker inspect shows the configured limits, and docker logs plus dmesg on the host show whether the kernel out-of-memory killer stepped in.

  • Container keeps getting killed (exit code 137): the docker memory limit is too low for the app’s real use, or the app leaks memory. Raise the limit in steps and watch docker stats over a full day before you call it fixed.
  • App is slow but not killed: check the docker cpu limit first. A container capped at 0.5 CPUs will feel slow under load even when the host has idle cores. Raise --cpus or move the heavy job to its own time window.
  • Limits seem to do nothing: the container was started before the flags were added, or the Compose file uses a deploy block that the local setup ignores. Recreate the container and confirm with docker inspect.
  • Host still runs out of memory: add up the limits of every container plus the host’s own services. Overcommitting limits only works when apps peak at different times.

Start with limits that match real use, not guesses. Give a web app the RAM it normally uses plus room for a traffic spike, give a database more than a cache-heavy worker, and recheck after a week. A good next step is to size the whole stack against the host: how much VPS you need for a self-hosting Docker stack walks through that math so your limits and your VPS plan agree with each other.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.