Docker CPU and Memory Limits
When you want to control the CPU and memory usage of Docker containers, you can use specific options in both Docker CLI commands and Docker Compose files. Here’s a detailed guide on how to use these options:
Docker CLI Commands
Setting CPU Limits:
- Option:
--cpus
- Usage: Specifies the number of CPUs that the container can use.
- Example:The
docker run --cpus="1.5" my-image
--cpus
option limits the container to 1.5 CPU cores.
- Option:
Setting Memory Limits:
Option:
--memory
or-m
Usage: Sets the maximum amount of memory the container can use.
Example:
docker run --memory="500m" my-image
This command limits the container to 500MB of RAM. If the limit is exceeded, the container will be throttled or terminated.
Note: The options take a positive integer followed by suffixes such as
b
,k
,m
, org
to indicate bytes, kilobytes, megabytes, or gigabytes.
Soft Memory Limits:
- Option:
--memory-reservation
- Example:This reserves 512MB of memory while setting a hard limit of 1GB.
docker run --memory="1g" --memory-reservation="512m" nginx
- Option:
Docker Compose Configuration
In Docker Compose, you can define resource limits under the deploy
section for version 3.x or directly under the services
section for version 2.x.
Setting CPU and Memory Limits in Docker Compose (Version 2.x):
version: '2' services: my-service: image: my-image deploy: resources: limits: cpus: '1.5' memory: 500M
This limits the service to 1.5 CPUs and 500MB of memory.
Setting CPU and Memory Limits in Docker Compose (Version 3.x and above):
version: '3.8' services: my-service: image: my-image deploy: resources: limits: cpus: '1.5' memory: '500M'
This limits the service to 1.5 CPUs and 500MB of memory.
Notes:
Swap Memory: You can enable swap memory using the
--memory-swap
option in CLI. For example:docker run --memory="512m" --memory-swap="1g" nginx
CPU Shares: Set CPU priorities between containers using
--cpu-shares
, with 1024 being the default.
By using these options, you can ensure that your Docker containers use resources efficiently, preventing them from overwhelming the host machine.