Limit Memory (RAM) and CPU in Docker Compose#
Set the values in Docker Compose#
version: '3.7'
services:
foo:
image: alpine:3
deploy:
resources:
limits:
cpus: '0.1'
memory: 100M
Find the right values#
There are many tools to choose from. Here’s what we use:
docker stats#
To get current values, simply run
docker stats
This is useful to get current memory and see spikes in CPU usage.
Prometheus Metrics#
CPUs [1] correlates with the Prometheus metric
container_cpu_user_seconds_total
as exposed by cAdvisor, e.g.
sum by (name) (rate(container_cpu_user_seconds_total{image!=""}[$__rate_interval]))
Note the usage of the variable rate_interval
(new Grafana
7.2).
Memory can be queried like this:
sum by (name) (container_memory_usage_bytes{image!=""})
Bemerkung
If you want a fully functional Prometheus and Grafana stack, be sure to check out hukudo/prom/
Ubuntu Kernel Cleanup
Bind mounts vs. Volumes in Docker Compose