Skip to content

13. Resource Management

By default, a Docker container has no resource constraints. If an application contains a memory leak or a runaway infinite loop, it can consume 100% of the host machine’s CPU and RAM, starving other containers and crashing the entire server.

To prevent “Noisy Neighbor” problems and Denial of Service (DoS) scenarios, you must explicitly manage and limit container resources using Linux cgroups (Control Groups).

Docker provides fine-grained control over how much processing power a container can utilize.

The most direct way to limit CPU usage. It places a hard cap on how much of the host’s CPU the container can use.

Terminal window
# Limit the container to at most 1.5 CPUs (1 and a half cores)
docker run --cpus="1.5" my-app

A relative weight for CPU time. By default, all containers have a share of 1024. This acts as a priority system only when the CPU is under heavy contention.

  • If Container A has 1024 shares, and Container B has 512 shares, Container A will receive twice as much CPU time as Container B, but only if they are both fighting for 100% of the CPU. If the CPU is idle, Container B can still use 100% of it.
Terminal window
docker run --cpu-shares="512" my-app

Before the --cpus flag was introduced, administrators managed CPU time in microseconds using periods and quotas.

  • --cpu-period: The length of the CPU cycle (usually 100,000 microseconds, or 100ms).
  • --cpu-quota: How many microseconds within that period the container is allowed to run. (Note: Use --cpus instead, as it is just a much easier wrapper around periods and quotas).

Memory limits are critical. If the host runs out of memory, the Linux kernel will start violently killing processes to survive.

Places a hard cap on the maximum amount of RAM the container can use. If the container tries to consume more than this limit, it will be killed.

Terminal window
# Limit the container to 512 Megabytes of RAM
docker run --memory="512m" my-app

A soft limit. Under normal operation, the container can use more than the reservation (up to its hard limit). However, if the host machine experiences a severe memory shortage, Docker will force the container to release memory down to this reservation level.

Terminal window
docker run --memory="1g" --memory-reservation="512m" my-app

If a container exceeds its hard --memory limit, the Linux Kernel’s OOM Killer intervenes. It instantly terminates the container process to save the host. If you run docker inspect on the dead container, you will see OOMKilled: true.

Swap is virtual memory on the hard drive. If a container runs out of RAM, it can overflow into Swap to avoid being OOM Killed. However, reading/writing to a hard drive is astronomically slower than RAM, which will severely degrade application performance.

Terminal window
# Set 512m of RAM, and an additional 512m of Swap (Total 1g)
docker run --memory="512m" --memory-swap="1g" my-app

Unmanaged disk usage will eventually bring down any Docker host.

The thin read-write layer of a running container grows as the application creates temporary files or logs. If this layer gets too large, it consumes the host’s /var/lib/docker/containers partition.

Old, unused, or dangling images pile up over time. If you use CI/CD to deploy a new image every day, you must actively prune the old images, or the host disk will fill up completely.

Persistent named volumes (which store database files) are never deleted automatically, even if the container is destroyed. You must manage and back these up, and explicitly delete them using docker volume prune if they are orphaned and no longer needed.

By default, Docker captures everything the container prints to stdout/stderr and saves it in a JSON file on the host. If your application is incredibly noisy, this log file will grow to hundreds of gigabytes and crash the server. Solution: Configure log rotation in the daemon.json or run command to restrict the size of log files.

Terminal window
docker run --log-opt max-size=10m --log-opt max-file=3 my-app

Visibility is essential for managing resources.

Provides a live, continuously updating terminal dashboard (like Linux top) showing exactly how much CPU, Memory, Network I/O, and Block I/O every running container is currently consuming.

Terminal window
docker stats

Analyzes the entire Docker installation on the host and displays exactly how much disk space is being consumed by Images, Containers, Local Volumes, and the Build Cache, as well as how much space is reclaimable.

Terminal window
docker system df
# Add -v for verbose output listing every specific item
docker system df -v