One advantage of Docker containers is that they allow you to restrict resources, such as CPU, memory, and I/O performance. In swarm mode clusters, you can restrict and reserve the resources for applications.
You can restrict and reserve container resources in the Container Service console by:
- Configuring interface parameters.
- Editing an orchestration template.
Note: Swarm mode clusters support Compose V1/V2 and Compose V3 orchestration templates. The syntaxes of resource constraint configuration in Compose V1/V2 and Compose V3 are different.
You can restrict and reserve resources when creating an application by using an image or changing the service configurations. For more information, see Create an application by using an image or Change service configurations.
In swarm mode clusters, 1.0 indicates a single CPU core and you can use decimals. Memory is measured in bytes.
|Compose V1/V2||Compose V3|
For more information, see resources.
A single CPU core is equivalent to 100 CPUs. If your machine is configured with 4 cores, the total number of available CPU resources is 400. In orchestration templates, you can use the
cpu_shares label to specify the CPU resources.
cpu_shares: 50 indicates 0.5 core.
You can use the
mem_limit label to restrict memory usage. Memory is measured in bytes and the minimum memory is 4 MB. If you set the memory limit and a container applies for a memory that exceeds the limit, the container is stopped because of OOM.
cpu_shares: 50 #0.5 core
mem_limit: 536870912 #512 MB
To guarantee that containers can obtain sufficient specified resources, such as 0.5 CPU core and 512 MB of memory in the preceding example, Container Service reserves resources for containers. For example, a 4-core machine can schedule up to eight
However, if you create containers without specifying the
mem_limit labels, Container Service does not reserve resources for such containers by default.
In Compose V3 orchestration templates, you can use the
limits label to specify the Resource Limit and use the
reservations label to specify the Resource Reserve.
For other resource limits, see Docker Compose instructions.