Restrict container resources

Last Updated: Dec 07, 2017

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.

Interface parameters

You can restrict resources when creating an application or changing the service configurations. For details, see Create an application or Change service configurations.

In swarm clusters, a single CPU core is equivalent to 100 CPUs. The unit of memory is MB.

1

Orchestration templates

In orchestration templates, you can use the cpu_shares and mem_limit labels to set CPU limit and memory limit.

CPU 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.

Memory limit

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.

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

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

Other resource limits

For other resource limits, see Docker Compose instructions.

Thank you! We've received your feedback.