One advantage of Docker containers is that they allow you to restrict resources, such as CPU, memory, and I/O performance. In swarm clusters, you can restrict the resources for applications.
You can restrict container resources in the Container Service console by:
- Setting interface parameters.
- Configuring settings in orchestration templates.
In swarm clusters, a single CPU core is equivalent to 100 CPUs. The unit of memory is MB.
In orchestration templates, you can use the
mem_limit labels to set CPU limit and memory limit.
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 CPU limit.
cpu_shares: 50 indicates 0.5 core.
You can use the
mem_limit label to restrict memory usage. The unit is the byte 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.
The following orchestration template demonstrates how to restrict CPU and memory.
cpu_shares: 50 #0.5 core
mem_limit: 536870912 #512 MB
To ensure 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
If you create containers without specifying the
mem_limit labels, Container Service does not reserve resources for such containers by default.
For other resource limits, see Docker Compose instructions.