📖 Scenario: You are managing a Docker container that runs a small web application. To keep the container stable and prevent it from using too much memory on the host machine, you want to set memory limits and reservations.This helps the container run smoothly without affecting other applications on the same server.
🎯 Goal: You will create a Docker container with specific memory limits and memory reservation settings. This will ensure the container uses memory efficiently and respects the host's resources.
📋 What You'll Learn
Create a Docker container running the nginx image
Set a memory limit of 300MB for the container
Set a memory reservation of 150MB for the container
Verify the container is running with the correct memory settings
💡 Why This Matters
🌍 Real World
Setting memory limits and reservations helps keep Docker containers from using too much memory, which can slow down or crash other applications on the same server.
💼 Career
Understanding resource limits is important for DevOps roles to ensure applications run reliably in shared environments.
Progress0 / 4 steps
1
Create a Docker container running nginx
Write the Docker command to create and run a container named mynginx using the nginx image in detached mode.
Docker
Hint
Use docker run with -d for detached mode and --name mynginx to name the container.
2
Add a memory limit of 300MB
Modify the Docker run command to add a memory limit of 300m using the --memory option.
Docker
Hint
Use --memory 300m to limit the container memory to 300 megabytes.
3
Add a memory reservation of 150MB
Extend the Docker run command to include a memory reservation of 150m using the --memory-reservation option.
Docker
Hint
Use --memory-reservation 150m to set the soft memory limit to 150 megabytes.
4
Verify the container memory settings
Write the Docker command to inspect the container mynginx and display only the memory limit and memory reservation values.
Docker
Hint
Use docker container inspect with --format to show memory settings.
Practice
(1/5)
1. What does setting a memory limit for a Docker container do?
easy
A. It sets the maximum memory the container can use.
B. It sets the minimum guaranteed memory for the container.
C. It disables memory usage for the container.
D. It sets the CPU usage limit for the container.
Solution
Step 1: Understand memory limit concept
A memory limit restricts the maximum RAM a container can consume to avoid overuse.
Step 2: Compare with other options
Memory reservation is the soft minimum, CPU limit is unrelated, disabling memory is invalid.
Final Answer:
It sets the maximum memory the container can use. -> Option A
5. You want to run a container that should always have at least 1GB of memory available but never use more than 2GB. Which Docker run options correctly enforce this?
hard
A. docker run --memory=1g --memory-reservation=2g mycontainer
B. docker run --memory-reservation=1g --memory=2g mycontainer
C. docker run --memory=2g --memory-reservation=1g mycontainer
D. docker run --memory-reservation=2g --memory=1g mycontainer
Solution
Step 1: Identify memory reservation and limit roles
Memory reservation sets soft minimum (1GB), memory limit sets hard max (2GB).
Step 2: Match options to roles
docker run --memory=2g --memory-reservation=1g mycontainer correctly sets --memory=2g (max) and --memory-reservation=1g (min).
Final Answer:
docker run --memory=2g --memory-reservation=1g mycontainer -> Option C
Quick Check:
Limit=2g, Reservation=1g for min and max [OK]
Hint: Memory limit is max, reservation is min; order matters [OK]
Common Mistakes:
Swapping limit and reservation values
Setting reservation higher than limit
Confusing units or flags
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.