Bird
Raised Fist0
Dockerdevops~10 mins

Memory limits and reservations in Docker - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a memory limit of 500MB for a Docker container.

Docker
docker run --memory=[1] myapp
Drag options to blanks, or click blank then click option'
A500MB
B500mb
C500m
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'MB' which Docker does not recognize.
Omitting the suffix, which defaults to bytes and is too small.
2fill in blank
medium

Complete the code to reserve 200MB of memory for a Docker container.

Docker
docker run --memory-reservation=[1] myapp
Drag options to blanks, or click blank then click option'
A200m
B200
C200mb
D200MB
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'MB' which is invalid.
Not specifying any suffix, which defaults to bytes.
3fill in blank
hard

Fix the error in the command to limit memory to 1GB.

Docker
docker run --memory=[1] myapp
Drag options to blanks, or click blank then click option'
A1g
B1G
C1GB
D1000m
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'GB' or 'G' which causes errors.
Using '1000m' which is 1000 megabytes, not exactly 1 gigabyte.
4fill in blank
hard

Fill both blanks to set a memory limit of 750MB and a reservation of 500MB.

Docker
docker run --memory=[1] --memory-reservation=[2] myapp
Drag options to blanks, or click blank then click option'
A750m
B500m
C750MB
D500MB
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'MB' which Docker does not accept.
Mixing suffixes or omitting them.
5fill in blank
hard

Fill all three blanks to set a memory limit of 2GB, a reservation of 1GB, and swap memory limit of 3GB.

Docker
docker run --memory=[1] --memory-reservation=[2] --memory-swap=[3] myapp
Drag options to blanks, or click blank then click option'
A2g
B1g
C3g
D3000m
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'G' or 'GB' suffixes.
Using megabytes instead of gigabytes for large values.

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