docker,

Docker tips: set memory and CPU limits

May 29, 2023 · 1 min read · Post a comment

Setting memory and CPU limits to Docker containers could prevent VM instances crashing – speaking from personal experience though. Here’s how to set it up.

Prerequisites

  • Docker

Solution

A docker run example:

docker run -d -p 80:80 --cpus=2 -m 512m --name redis redis:alpine

And, the same thing with Docker Compose:

version: "3.9"

services:
  redis:
    image: redis:alpine
    deploy:
      resources:
        limits:
          cpus: 2
          memory: 512M
        reservations:
          cpus: 1
          memory: 256M

Note: limits set an upper boundary on resource consumption, think of it as a hard limit, while reservations specify the minimum resources required for the container to run.

Conclusion

In case you face any issues, feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.