Bird
Raised Fist0
Dockerdevops~5 mins

Memory limits and reservations in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes, a container uses too much memory and slows down or crashes your system. Memory limits and reservations help control how much memory a container can use, keeping your system stable and fair for all containers.
When you want to make sure a container never uses more than a certain amount of memory to avoid crashing the host.
When you want to guarantee a minimum amount of memory is always available to a container for smooth operation.
When running multiple containers on one server and you want to prevent one container from using all memory.
When testing how your app behaves under memory pressure by setting strict limits.
When you want to optimize resource use by reserving memory but allowing containers to use more if available.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  my-app:
    image: nginx:1.23
    deploy:
      resources:
        limits:
          memory: 200M
        reservations:
          memory: 100M

This docker-compose file runs an nginx container named my-app.

The limits.memory sets the maximum memory the container can use to 200 megabytes.

The reservations.memory guarantees that 100 megabytes of memory will be reserved for this container.

This helps balance memory use and stability.

Commands
This command starts the container in detached mode using the docker-compose.yml file, applying the memory limits and reservations defined.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating my-app ... done
→
-d - Run containers in the background (detached mode)
This command shows the current memory usage and limits of the running container named my-app to verify the memory settings.
Terminal
docker stats --no-stream my-app
Expected OutputExpected
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS abc123def456 my-app 0.00% 15.5MiB / 200MiB 7.75% 1.2kB / 0B 0B / 0B 2
→
--no-stream - Show a single snapshot of stats instead of continuous updates
This command checks the memory limit set on the container in bytes to confirm the limit is applied.
Terminal
docker inspect my-app --format='{{json .HostConfig.Memory}}'
Expected OutputExpected
209715200
This command stops and removes the container and network created by docker-compose to clean up the environment.
Terminal
docker-compose down
Expected OutputExpected
Stopping my-app ... done Removing my-app ... done Removing network default
Key Concept

If you remember nothing else from this pattern, remember: memory limits cap the max memory a container can use, while reservations guarantee a minimum memory allocation.

Common Mistakes
Setting only memory limits without reservations
The container might not get guaranteed memory, causing performance issues under load.
Always set reservations to ensure minimum memory availability if your app needs stable performance.
Setting memory limits too low causing container crashes
If the limit is below what the app needs, the container will be killed by the system.
Set limits based on your app's memory needs plus some buffer.
Using memory limits without understanding units
Docker expects memory values in bytes or with suffixes like M for megabytes; wrong units cause errors or ignored limits.
Always specify memory with units like 200M or 1G to avoid confusion.
Summary
Use docker-compose.yml to set memory limits and reservations for containers.
Run containers with docker-compose up -d to apply these settings.
Check memory usage and limits with docker stats and docker inspect.
Stop and clean containers with docker-compose down.

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