All Products
Document Center

Restrict and reserve container resources

Last Updated: Feb 12, 2018

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.

Interface parameters

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.


Orchestration templates

Template version

Compose V1/V2 Compose V3
The mem_limit label.
The cpu_shares label.
Use the limits label to configure the Resource Limit.
Use the reservations label to configure the Resource Reserve.
For more information, see resources.

Compose V1/V2

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.

Orchestration example:

  1. n1:
  2. expose:
  3. - 443/tcp
  4. - 80/tcp
  5. image: 'nginx:latest'
  6. cpu_shares: 50 #0.5 core
  7. mem_limit: 536870912 #512 MB
  8. labels:
  9. aliyun.scale: '1'
  10. restart: always
  11. volumes:
  12. - /var/cache/nginx

Resource scheduling

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 cpu_shares=50 containers.

However, if you create containers without specifying the cpu_shares and mem_limit labels, Container Service does not reserve resources for such containers by default.

Compose V3

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.

Orchestration example:

  1. version: '3'
  2. n1:
  3. expose:
  4. - 443/tcp
  5. - 80/tcp
  6. image: 'nginx:latest'
  7. deploy:
  8. resources: #Note
  9. limits:
  10. cpus: '0.5'
  11. memory: 512M
  12. reservations:
  13. cpus: '0.3'
  14. memory: 300M
  15. restart: always
  16. volumes:
  17. - /var/cache/nginx

Other resource limits

For other resource limits, see Docker Compose instructions.