Bird
Raised Fist0
Dockerdevops~20 mins

Memory limits and reservations in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Memory Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Understanding Docker Memory Limit Output
What is the output of the following Docker command when inspecting a container's memory limits?
Docker
docker inspect --format='{{.HostConfig.Memory}}' my_container
A1073741824
B0
C536870912
DMemory limit not set
Attempts:
2 left
💡 Hint
If no memory limit is set, Docker returns 0 for the Memory field.
🧠 Conceptual
intermediate
2:00remaining
Difference Between Memory Limit and Memory Reservation
Which statement correctly describes the difference between Docker's memory limit and memory reservation?
AMemory limit is a hard cap; memory reservation is a soft guarantee.
BMemory limit applies only to CPU, memory reservation applies only to disk.
CBoth memory limit and reservation are hard caps on memory usage.
DMemory reservation is a hard cap; memory limit is a soft guarantee.
Attempts:
2 left
💡 Hint
Think about which setting restricts usage strictly and which one reserves resources.
❓ Configuration
advanced
2:30remaining
Correct Docker Compose Memory Settings
Which Docker Compose service configuration correctly sets a memory limit of 512MB and a memory reservation of 256MB?
Docker
services:
  app:
    image: myapp
    deploy:
      resources:
        limits:
          memory: 512M
        reservations:
          memory: 256M
A
services:
  app:
    image: myapp
    deploy:
      resources:
        limits:
          memory: 512mb
        reservations:
          memory: 256mb
B
services:
  app:
    image: myapp
    deploy:
      resources:
        limits:
          memory: 512MB
        reservations:
          memory: 256MB
C
services:
  app:
    image: myapp
    deploy:
      resources:
        limits:
          memory: 512
        reservations:
          memory: 256
D
services:
  app:
    image: myapp
    deploy:
      resources:
        limits:
          memory: 512M
        reservations:
          memory: 256M
Attempts:
2 left
💡 Hint
Memory values in Docker Compose use uppercase 'M' for megabytes without 'B'.
❓ Troubleshoot
advanced
2:30remaining
Diagnosing Memory Limit Enforcement Failure
A container is set with a memory limit of 256MB but uses more memory without being killed. What is the most likely cause?
AThe container is running with the --memory-swap flag set to unlimited.
BThe container was started without the --memory flag.
CThe host machine has swap disabled.
DThe container is using the memory reservation setting instead of limit.
Attempts:
2 left
💡 Hint
Check if swap settings affect memory limit enforcement.
🔀 Workflow
expert
3:00remaining
Order of Steps to Set Memory Limits on a Running Container
What is the correct order of steps to apply a memory limit of 1GB to an already running Docker container named 'webapp'?
A1,4,2,3
B2,1,3,4
C1,2,3,4
D4,1,2,3
Attempts:
2 left
💡 Hint
You cannot change memory limits on a running container without restarting it.

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

  1. Step 1: Understand memory limit concept

    A memory limit restricts the maximum RAM a container can consume to avoid overuse.
  2. Step 2: Compare with other options

    Memory reservation is the soft minimum, CPU limit is unrelated, disabling memory is invalid.
  3. Final Answer:

    It sets the maximum memory the container can use. -> Option A
  4. Quick Check:

    Memory limit = max memory allowed [OK]
Hint: Memory limit caps max RAM usage, reservation sets minimum [OK]
Common Mistakes:
  • Confusing memory limit with memory reservation
  • Thinking memory limit affects CPU
  • Assuming memory limit disables memory
2. Which of the following is the correct Docker run command syntax to set a memory limit of 500MB?
easy
A. docker run --mem=500MB myimage
B. docker run --memory=500m myimage
C. docker run --memory-limit=500m myimage
D. docker run --mem-limit=500MB myimage

Solution

  1. Step 1: Recall Docker memory limit flag

    The correct flag is --memory followed by the size with unit, e.g., 500m for 500MB.
  2. Step 2: Check syntax correctness

    Options A, B, and C use incorrect flag names or units.
  3. Final Answer:

    docker run --memory=500m myimage -> Option B
  4. Quick Check:

    Use --memory=500m for 500MB limit [OK]
Hint: Use --memory=500m to set 500MB limit correctly [OK]
Common Mistakes:
  • Using wrong flag names like --mem-limit
  • Using uppercase units like MB instead of m
  • Omitting the equal sign
3. Given the command docker run --memory=300m --memory-reservation=200m myapp, what happens if the container tries to use 350MB of memory?
medium
A. The container is killed because it exceeded the 300MB limit.
B. The container is allowed to use 350MB without restrictions.
C. The container is throttled but not killed immediately.
D. The container automatically increases its memory reservation.

Solution

  1. Step 1: Understand memory limit vs reservation

    Memory limit (300MB) is the hard max; reservation (200MB) is soft minimum.
  2. Step 2: Analyze container memory usage

    Using 350MB exceeds the 300MB limit, so Docker kills the container.
  3. Final Answer:

    The container is killed because it exceeded the 300MB limit. -> Option A
  4. Quick Check:

    Memory limit exceeded = container killed [OK]
Hint: Memory limit is max; exceeding it kills container [OK]
Common Mistakes:
  • Confusing reservation with limit
  • Thinking container is throttled instead of killed
  • Assuming reservation auto-increases
4. You set --memory-reservation=400m but your container keeps getting killed when using 350MB. What is the likely cause?
medium
A. The container is using less memory than reservation, so it is killed.
B. Memory reservation kills container if usage is below it.
C. Docker does not support memory reservation on your system.
D. You forgot to set a memory limit, so default limit is too low.

Solution

  1. Step 1: Understand memory reservation role

    Memory reservation is a soft minimum, it does not kill containers.
  2. Step 2: Identify missing memory limit effect

    Without a memory limit, Docker may apply a default low limit causing kills.
  3. Final Answer:

    You forgot to set a memory limit, so default limit is too low. -> Option D
  4. Quick Check:

    Missing memory limit causes unexpected kills [OK]
Hint: Always set memory limit; reservation alone won't prevent kills [OK]
Common Mistakes:
  • Thinking reservation kills containers
  • Ignoring default memory limits
  • Assuming reservation enforces max memory
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

  1. Step 1: Identify memory reservation and limit roles

    Memory reservation sets soft minimum (1GB), memory limit sets hard max (2GB).
  2. Step 2: Match options to roles

    docker run --memory=2g --memory-reservation=1g mycontainer correctly sets --memory=2g (max) and --memory-reservation=1g (min).
  3. Final Answer:

    docker run --memory=2g --memory-reservation=1g mycontainer -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard