Resource limits in Docker are settings that control how much CPU, memory, and other resources a container can use to prevent it from using too much and affecting other containers or the host system.
Click to reveal answer
beginner
Why is it important to set memory limits on Docker containers?
Setting memory limits prevents a container from using all the host's memory, which can cause the system to slow down or crash. It helps keep the system stable and fair for all containers.
Click to reveal answer
intermediate
What happens if a Docker container exceeds its CPU limit?
If a container exceeds its CPU limit, Docker will restrict its CPU usage, slowing down the container's processes to keep the host system responsive and fair to other containers.
Click to reveal answer
intermediate
How do resource limits help in a multi-container environment?
Resource limits ensure that no single container hogs all the resources, allowing multiple containers to run smoothly together without one causing others to slow down or fail.
Click to reveal answer
beginner
Name two Docker flags used to set resource limits.
Two common Docker flags for resource limits are --memory to limit memory usage and --cpus to limit CPU usage.
Click to reveal answer
What is the main reason to set resource limits on Docker containers?
ATo prevent containers from using too many resources and affecting others
BTo make containers run faster than the host
CTo allow containers to use unlimited resources
DTo disable container networking
✗ Incorrect
Resource limits prevent containers from using excessive resources, which helps keep the system stable and fair.
Which Docker flag limits the memory a container can use?
A--memory
B--cpus
C--network
D--storage
✗ Incorrect
The --memory flag sets the maximum memory a container can use.
If a container uses more CPU than its limit, what happens?
ADocker kills the container immediately
BDocker allows unlimited CPU usage
CDocker slows down the container's CPU usage
DDocker increases the host CPU speed
✗ Incorrect
Docker restricts the container's CPU usage to keep the system responsive.
Why are resource limits especially important in multi-container setups?
ATo make containers communicate faster
BTo prevent one container from starving others of resources
CTo increase container storage
DTo disable container logs
✗ Incorrect
Resource limits ensure fair resource sharing among containers.
Which resource is NOT typically limited by Docker resource flags?
AMemory
BCPU
CDisk I/O
DNetwork bandwidth
✗ Incorrect
Docker does not provide built-in flags to limit network bandwidth directly.
Explain why setting resource limits on Docker containers helps maintain system stability.
Think about what happens if one container uses all memory or CPU.
You got /4 concepts.
Describe how you would use Docker flags to limit a container's CPU and memory usage.
Use --memory to limit RAM and --cpus to limit CPU cores.
You got /4 concepts.
Practice
(1/5)
1. Why is it important to set resource limits like --memory and --cpus when running Docker containers?
easy
A. To prevent one container from using too many resources and slowing down the whole system
B. To make containers run faster by giving them unlimited resources
C. To allow containers to access the host's hardware directly
D. To automatically update the container software
Solution
Step 1: Understand resource limits purpose
Resource limits control how much CPU and memory a container can use to avoid overloading the host system.
Step 2: Identify the effect of limits
Setting limits prevents one container from hogging all resources, keeping the system stable for all containers.
Final Answer:
To prevent one container from using too many resources and slowing down the whole system -> Option A
Quick Check:
Resource limits = prevent overload [OK]
Hint: Limits protect system by capping container resource use [OK]
Common Mistakes:
Thinking limits make containers faster
Believing containers get direct hardware access
Confusing resource limits with software updates
2. Which of the following is the correct syntax to limit a Docker container's memory to 500MB and CPU to 1.5 cores?
easy
A. docker run --memory=500MB --cpus=1.5 myimage
B. docker run --mem=500 --cpu=1.5 myimage
C. docker run --memory-limit=500MB --cpu-limit=1.5 myimage
D. docker run --memory 500MB --cpus 1.5 myimage
Solution
Step 1: Recall Docker resource limit flags
The correct flags are --memory for memory and --cpus for CPU limits.
Step 2: Check syntax correctness
docker run --memory=500MB --cpus=1.5 myimage uses correct flags with equal signs and units, which is the proper Docker syntax.
Final Answer:
docker run --memory=500MB --cpus=1.5 myimage -> Option A
Quick Check:
Correct flags and syntax = docker run --memory=500MB --cpus=1.5 myimage [OK]
Hint: Use --memory and --cpus with = and units [OK]
Common Mistakes:
Using incorrect flag names like --mem or --cpu
Omitting equal signs between flag and value
Not specifying units for memory
3. What will happen if you run this command?
docker run --memory=100m --cpus=0.5 busybox sh -c "stress --vm 1 --vm-bytes 200m --vm-hang 0"
medium
A. The container will ignore the limits and run with full resources
B. The container will run normally without any issues
C. The container will use more CPU than allowed but not memory
D. The container will be killed because it tries to use more memory than allowed
Solution
Step 1: Analyze memory limit and stress command
The container is limited to 100MB memory but tries to allocate 200MB, which exceeds the limit.
Step 2: Understand Docker behavior on limit breach
Docker kills containers that exceed memory limits to protect the host system.
Final Answer:
The container will be killed because it tries to use more memory than allowed -> Option D
Quick Check:
Memory limit exceeded = container killed [OK]
Hint: Exceeding memory limit kills container [OK]
Common Mistakes:
Assuming container runs normally despite limit
Thinking CPU limits affect memory usage
Believing limits are ignored
4. You set --memory=256m on a container, but it still crashes with an out-of-memory error. What is the most likely cause?
medium
A. The container is using swap memory which is unlimited by default
B. The container is running on a host with too much free memory
C. The memory limit flag was misspelled and ignored
D. The CPU limit is too low causing memory errors
Solution
Step 1: Check common mistakes with memory limits
If the flag is misspelled, Docker ignores it and no limit is applied, causing crashes.
Step 2: Rule out other options
Swap is limited separately, host memory does not cause container crashes, and CPU limits do not cause memory errors.
Final Answer:
The memory limit flag was misspelled and ignored -> Option C
Quick Check:
Misspelled flag = no limit applied [OK]
Hint: Check flag spelling if limits don't work [OK]