This error occurs when you set Docker's --memory-reservation value higher than the --memory (hard limit). Memory reservation is a soft limit that must always be lower than or equal to the hard memory limit.
The "Memory reservation cannot be larger than memory limit" error occurs when Docker validates your memory settings and finds that the soft limit (--memory-reservation) exceeds the hard limit (--memory or -m). In Docker's memory model, there are two types of limits: - **Hard limit (--memory)**: The maximum amount of memory a container can use. If the container tries to exceed this, it will be killed (OOM). - **Soft limit (--memory-reservation)**: A target that Docker tries to maintain when memory is contended on the host. The container can use more than this if memory is available, but during memory pressure, Docker will attempt to shrink the container's usage to this reservation. Since the soft limit is meant to be a "floor" that the container can exceed up to the hard limit, it makes no logical sense for the reservation to be higher than the maximum allowed memory. Docker enforces this constraint and refuses to start the container if violated.
Before fixing, understand the two settings:
- `--memory` (hard limit): Maximum memory the container can use. Going over this triggers OOM killer.
- `--memory-reservation` (soft limit): Target memory during host memory pressure. Container can exceed this when memory is available.
The rule: reservation must be <= limit
Example of invalid configuration:
# WRONG: reservation (500M) > limit (256M)
docker run --memory=256m --memory-reservation=500m nginxExample of valid configuration:
# CORRECT: reservation (200M) < limit (500M)
docker run --memory=500m --memory-reservation=200m nginxEnsure your --memory-reservation is less than --memory:
# Set a 1GB limit with 512MB reservation
docker run -d \
--name myapp \
--memory=1g \
--memory-reservation=512m \
myimage:latestCommon memory value formats:
- b - bytes
- k - kilobytes
- m - megabytes
- g - gigabytes
A good rule of thumb: set reservation to 50-75% of the limit. This gives the container headroom to burst while ensuring resources are reclaimed during memory pressure.
For Docker Compose files, check both the legacy and modern syntax:
Modern syntax (Compose v3.x with deploy):
services:
myapp:
image: myimage:latest
deploy:
resources:
limits:
memory: 1G
reservations:
memory: 512M # Must be <= limits.memoryLegacy syntax (Compose v2.x):
services:
myapp:
image: myimage:latest
mem_limit: 1g
mem_reservation: 512m # Must be <= mem_limitCompose v3.9+ shorthand:
services:
myapp:
image: nginx
mem_limit: 250M
mem_reservation: 100M # Must be <= mem_limitNote: For deploy section to work outside Swarm mode, use the --compatibility flag:
docker-compose --compatibility up -dCheck the memory configuration of an existing container:
# View memory limit (returns bytes, 0 = unlimited)
docker inspect --format='{{.HostConfig.Memory}}' <container>
# View memory reservation (returns bytes)
docker inspect --format='{{.HostConfig.MemoryReservation}}' <container>
# View both settings formatted
docker inspect <container> | grep -E "Memory|MemoryReservation"Example output:
"Memory": 536870912, // 512MB limit
"MemoryReservation": 268435456 // 256MB reservationTo convert bytes to megabytes, divide by 1048576 (1024^2).
If you want a soft limit without a hard cap, you have two options:
Option 1: Set a very high hard limit
docker run -d \
--memory=8g \
--memory-reservation=512m \
myimage:latestOption 2: Omit the hard limit entirely
# Only sets reservation, no hard limit
docker run -d \
--memory-reservation=512m \
myimage:latestWith no --memory flag, the container can use as much memory as available on the host, but Docker will try to reclaim down to the reservation during memory pressure.
Note: Running without a hard limit is risky in production as a memory leak could affect other containers or the host.
How memory reservation actually works: Memory reservation is enforced through Linux cgroups' soft_limit_in_bytes. When the host experiences memory pressure (low free memory or high page cache demand), the kernel's memory reclaim process will target containers to shrink them toward their reservation value.
Docker Swarm scheduling: In Swarm mode, --reserve-memory affects scheduling decisions. Swarm will only place a task on a node with at least that much memory available. However, it does NOT enforce the reservation at runtime - use --limit-memory for that.
No effect warning: On some systems or Docker Compose versions, memory reservation may be silently ignored. You can verify it was applied:
docker inspect <container> | grep MemoryReservationIf it shows 0, the setting was not applied. This can happen with:
- Docker Compose v3.x without Swarm mode (use --compatibility flag)
- Older Docker versions
- Kernels without cgroup memory controller
Relationship with swap: Memory reservation only applies to RAM, not swap. If you set --memory-swap equal to --memory, swap is disabled and the reservation applies strictly to physical memory usage.
Kubernetes equivalent: In Kubernetes, this concept maps to:
resources:
requests:
memory: "256Mi" # Similar to --memory-reservation
limits:
memory: "512Mi" # Similar to --memoryKubernetes also enforces that requests cannot exceed limits.
dockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Error response from daemon: manifest for nginx:nonexistent not found: manifest unknown: manifest unknown
How to fix 'manifest for image:tag not found' in Docker
Error response from daemon: invalid reference format: repository name must be lowercase
How to fix 'repository name must be lowercase' in Docker
Error response from daemon: No such image
How to fix 'No such image' in Docker
Error response from daemon: Container is not running
How to fix 'Container is not running' when using docker exec