Limit RAM for Docker Compose Containers
Add this inside each service in docker-compose.yml, at the same level as image:
services:
grafana:
image: grafana/grafana
deploy:
resources:
limits:
memory: 512M
reservations:
memory: 128M
- limits: hard cap. If the container goes over, it's killed (OOM).
- reservations: minimum it should get when the host is short of memory.
Docker Compose v2 applies these without Swarm. Apply and check:
docker compose up -d
docker stats --no-stream
docker inspect --format '{{.State.OOMKilled}}' grafana
Values I use (adjust to your load):
| Container | Reservation | Limit |
|---|---|---|
| Prometheus | 256M | 1G |
| Grafana | 128M | 512M |
| cAdvisor | 128M | 300M |
| Nexterm | 128M | 512M |
| Cloudflared | 64M | 128M |
| Watchtower | 32M | 128M |
| Node Exporter | 32M | 64M |