0
0
Dockerdevops~5 mins

Why resource limits matter in Docker - Why It Works

Choose your learning style9 modes available
Introduction
When you run applications in containers, they can use too much memory or CPU and slow down or crash your computer. Setting resource limits helps keep your system stable by controlling how much CPU and memory each container can use.
When you want to prevent one container from using all the CPU and slowing down other containers.
When you need to avoid a container crashing the host by using too much memory.
When running multiple containers on the same machine and you want to share resources fairly.
When testing how your app behaves under limited CPU or memory conditions.
When deploying apps in production to ensure reliability and avoid unexpected crashes.
Commands
This command runs a container named 'limited-container' with a memory limit of 100 megabytes and CPU limit of half a CPU core. It runs the 'top' command to show running processes inside the container.
Terminal
docker run --name limited-container --memory 100m --cpus 0.5 busybox top
Expected OutputExpected
top - 00:00:00 up 0 min, 0 users, load average: 0.00, 0.00, 0.00 Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND 0.0 0.0 1232 512 pts/0 R+ 00:00 0:00 top
--memory - Limits the maximum memory the container can use
--cpus - Limits the number of CPU cores the container can use
This command shows the current CPU and memory usage of the 'limited-container' to verify the limits are in effect.
Terminal
docker stats limited-container --no-stream
Expected OutputExpected
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS abcdef123456 limited-container 0.00% 2.5MiB / 100MiB 2.50% 1.2kB / 0B 0B / 0B 1
--no-stream - Shows a single snapshot of resource usage instead of continuous updates
This command stops and removes the 'limited-container' to clean up after the test.
Terminal
docker rm -f limited-container
Expected OutputExpected
limited-container
-f - Forces removal of the container even if it is running
Key Concept

If you remember nothing else from this pattern, remember: setting resource limits on containers prevents them from using too much CPU or memory and keeps your system stable.

Common Mistakes
Not setting any resource limits when running containers
Containers can consume all available CPU or memory, causing the host or other containers to slow down or crash.
Always use --memory and --cpus flags to set reasonable limits based on your app's needs.
Setting resource limits too low without testing
Your container might not have enough resources to run properly and could crash or behave unexpectedly.
Test your app with different limits to find a balance between resource use and performance.
Confusing --memory with swap limits
By default, --memory limits only RAM, and containers can still use swap space, which might cause unexpected memory use.
Understand Docker's memory and swap settings and configure swap limits if needed for stricter control.
Summary
Use --memory and --cpus flags to limit container resource usage.
Check resource usage with docker stats to verify limits.
Remove containers after testing to keep your environment clean.