0
0
Dockerdevops~5 mins

Memory limits and reservations in Docker - Commands & Configuration

Choose your learning style9 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.