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).
13.1 CPU Management
Section titled “13.1 CPU Management”Docker provides fine-grained control over how much processing power a container can utilize.
CPU Limits
Section titled “CPU Limits”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.
# Limit the container to at most 1.5 CPUs (1 and a half cores)docker run --cpus="1.5" my-appCPU Shares
Section titled “CPU Shares”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
1024shares, and Container B has512shares, 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.
docker run --cpu-shares="512" my-appCPU Quotas (Advanced)
Section titled “CPU Quotas (Advanced)”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--cpusinstead, as it is just a much easier wrapper around periods and quotas).
13.2 Memory Management
Section titled “13.2 Memory Management”Memory limits are critical. If the host runs out of memory, the Linux kernel will start violently killing processes to survive.
Memory Limits
Section titled “Memory Limits”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.
# Limit the container to 512 Megabytes of RAMdocker run --memory="512m" my-appMemory Reservations
Section titled “Memory Reservations”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.
docker run --memory="1g" --memory-reservation="512m" my-appOOM Killer (Out Of Memory)
Section titled “OOM Killer (Out Of Memory)”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.
# Set 512m of RAM, and an additional 512m of Swap (Total 1g)docker run --memory="512m" --memory-swap="1g" my-app13.3 Disk Management
Section titled “13.3 Disk Management”Unmanaged disk usage will eventually bring down any Docker host.
Container Storage
Section titled “Container Storage”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.
Image Storage
Section titled “Image Storage”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.
Volume Storage
Section titled “Volume Storage”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.
docker run --log-opt max-size=10m --log-opt max-file=3 my-app13.4 Monitoring
Section titled “13.4 Monitoring”Visibility is essential for managing resources.
docker stats
Section titled “docker stats”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.
docker statsdocker system df
Section titled “docker system df”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.
docker system df# Add -v for verbose output listing every specific itemdocker system df -v