docker
docker-compose
resource-limitation
memory-limit
cpu-limit

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.

Practice system design

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.

yaml
1version: '3.0'
2
3services:
4  web:
5    image: nginx
6    deploy:
7      resources:
8        limits:
9          cpus: '0.50'
10          memory: 128M
11        reservations:
12          cpus: '0.25'
13          memory: 64M

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.5 for half-core usage).
  • Shares: This is supported in older docker-compose versions for weight-based allocation, though not recommended for version 3 where direct limits are preferable.

Example

yaml
1deploy:
2  resources:
3    limits:
4      cpus: '1.0'

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 M for megabytes or G for gigabytes.
  • Swap: Memory and swap can be individually specified to handle paging.

Example

yaml
1deploy:
2  resources:
3    limits:
4      memory: 512M

This limits the container to using a maximum of 512 megabytes of memory.

Best Practices

  1. Understand Your Application Requirements:
    • Analyze the resource requirements of your applications and set constraints accordingly to avoid over- or under-utilizing hardware.
  2. Testing:
    • Test under different load conditions to ensure containers have adequate resources and adjust the limits as needed.
  3. Prioritize Critical Services:
    • Use reservations for mission-critical services to guarantee they have the necessary resources, especially in a clustered environment.

Key Points Summary

FeatureDescriptionExample
CPU LimitsRestrict the CPU usage of a containercpus: '0.50' (50% of a CPU)
Memory LimitsRestrict the memory usage of a containermemory: 256M
ReservationsGuarantee a certain amount of CPU and memory for a taskcpus: '0.25' memory: 64M
Version SupportApplies to Docker Compose version 3 and aboveApplicable from version 3.0
UnitsMemory uses M/G; CPU uses fractional values for coresUse 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.