How to specify Memory CPU limit in docker compose version 3
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Docker Compose is a powerful tool for defining and managing multi-container Docker applications. In Docker Compose version 3, specifying resource limits such as CPU and memory is essential for optimizing application performance and ensuring that resources are distributed effectively. This article explores how to configure CPU and memory limits in Docker Compose version 3, providing technical explanations and examples for clear understanding.
Understanding Docker's Resource Management
Docker uses cgroups (control groups) to limit the resources available to a container. Properly setting these resources can prevent any single container from monopolizing system resources, leading to more predictable performance and stability.
Setting Up Resource Limits in Docker Compose
Docker Compose version 3 supports CPU and memory constraints through the deploy key. Here’s a technical breakdown of specifying these limits.
Basic Syntax
The deploy key within a service configuration section allows you to specify the resources directive that includes limits and reservations.
Key Concepts
limits: This specifies the maximum amount of resources (CPU and memory) that a container can use.reservations: These set aside a specific amount of resources, ensuring that this much resource is available for the container, which is particularly useful in a cluster with multiple containers.
CPU Constraints
The CPU limit can be defined using either:
- Cores: Fractional values indicate the proportion of a CPU core (e.g.,
0.5for half-core usage). - Shares: This is supported in older
docker-composeversions for weight-based allocation, though not recommended for version 3 where direct limits are preferable.
Example
In this example, the container is limited to using a maximum of 1 CPU core.
Memory Constraints
Memory constraints are defined using:
- Bytes: Values can be suffixed with
Mfor megabytes orGfor gigabytes. - Swap: Memory and swap can be individually specified to handle paging.
Example
This limits the container to using a maximum of 512 megabytes of memory.
Best Practices
- Understand Your Application Requirements:
- Analyze the resource requirements of your applications and set constraints accordingly to avoid over- or under-utilizing hardware.
- Testing:
- Test under different load conditions to ensure containers have adequate resources and adjust the limits as needed.
- Prioritize Critical Services:
- Use reservations for mission-critical services to guarantee they have the necessary resources, especially in a clustered environment.
Key Points Summary
| Feature | Description | Example |
| CPU Limits | Restrict the CPU usage of a container | cpus: '0.50' (50% of a CPU) |
| Memory Limits | Restrict the memory usage of a container | memory: 256M |
| Reservations | Guarantee a certain amount of CPU and memory for a task | cpus: '0.25'
memory: 64M |
| Version Support | Applies to Docker Compose version 3 and above | Applicable from version 3.0 |
| Units | Memory uses M/G; CPU uses fractional values for cores | Use M for MB and G for GB |
Conclusion
Setting resource limits for CPU and memory in Docker Compose version 3 is crucial for maintaining efficient resource utilization and stable application performance. By defining these constraints under the deploy section, you can ensure that your Docker services remain responsive and do not exhaust host system resources. Always complement resource constraints with thorough testing and monitoring to fine-tune the resource allocation according to your specific use-case needs.
Related reading
- How to SSH to docker container in kubernetes cluster?
- How to start a stopped Docker container with a different command?
- How to start apache2 automatically in a ubuntu docker container?
- How to stop all containers when one container stops with docker-compose?
- How to stop Docker and Kubernetes using Docker desktop?
- How to stop docker under Linux
- how to stop/pause a pod in kubernetes
- How to switch namespace in kubernetes

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.