0
0
Dockerdevops~10 mins

Memory limits and reservations in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Memory limits and reservations
Start Container
Check Memory Reservation
Reserve Memory for Container
Check Memory Usage
If Usage < Limit
If Usage > Limit
Container Stops or Continues
Docker starts a container, reserves specified memory, monitors usage, and enforces limits by killing the container if it exceeds the limit.
Execution Sample
Docker
docker run --memory=500m --memory-reservation=200m alpine sleep 60
Runs an Alpine container with 500MB max memory and 200MB reserved memory, then sleeps for 60 seconds.
Process Table
StepActionMemory ReservedMemory LimitMemory UsageResult
1Start container200MB500MB0MBContainer starts with reserved memory set
2Container runs workload200MB500MB150MBUsage below reservation, normal operation
3Container runs heavier workload200MB500MB450MBUsage below limit, container continues
4Container tries to exceed limit200MB500MB520MBUsage exceeds limit, container killed
5Container is killed200MB500MBN/AContainer stopped
💡 Container is killed when memory usage exceeds the 500MB limit
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Memory Reserved200MB200MB200MB200MB200MB
Memory Limit500MB500MB500MB500MB500MB
Memory Usage0MB150MB450MB520MBN/A
Key Moments - 3 Insights
Why does the container continue running when memory usage is above the reservation but below the limit?
Because memory reservation is a soft guarantee, the container can use more memory up to the limit without being stopped, as shown in step 3 of the execution table.
What happens when memory usage exceeds the memory limit?
The container is killed to prevent it from using more memory than allowed, as shown in step 4 of the execution table.
Is the reserved memory immediately allocated to the container at start?
No, reserved memory is a soft guarantee, not a hard allocation. The container can start with less usage and grow up to the reservation without issues, as seen in step 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the memory usage at step 3?
A150MB
B520MB
C450MB
D200MB
💡 Hint
Check the 'Memory Usage' column in row for step 3 in the execution table.
At which step does the container exceed its memory limit?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look for when 'Memory Usage' surpasses 'Memory Limit' in the execution table.
If the memory reservation was increased to 300MB, how would step 2's memory reserved value change?
AIt would change to 300MB
BIt would stay 200MB
CIt would change to 500MB
DIt would become 0MB
💡 Hint
Refer to the 'Memory Reserved' column in the variable tracker and relate it to reservation settings.
Concept Snapshot
Docker memory limits and reservations:
- --memory sets the max memory a container can use (hard limit).
- --memory-reservation sets a soft memory guarantee.
- Container can use memory between reservation and limit but risks killing if exceeding limit.
- Reservation helps Docker manage resources without immediate hard allocation.
- Exceeding limit triggers container termination.
Full Transcript
This visual execution shows how Docker manages memory for containers using memory limits and reservations. When a container starts, Docker reserves a soft memory amount and sets a hard memory limit. The container can use memory up to the reservation without issues. If usage grows beyond reservation but stays below the limit, the container continues running normally. If memory usage exceeds the limit, Docker kills the container to protect the host system. The execution table tracks memory reserved, limit, and usage step-by-step, showing container behavior at each stage. Key moments clarify common confusions about soft reservation versus hard limits. The quiz tests understanding of memory usage at different steps and the effect of changing reservation values.