Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to allocate memory and cpu to a docker container

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to allocate memory and cpu to docker containers". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

By default, the resources used by the container are unrestricted. That is, the maximum resources allowed by the host kernel scheduler can be used. However, in the process of using a container, it is often necessary to limit the host resources that the container can use. Let's introduce it below.

Container CPU Settings

By default, all containers can use host CPU resources equally and without restrictions. Docker can set the weight of the container's use of CPU with-c or-cpu-shares. If not specified, the default value is 1024.

Unlike the memory limit, the cpu share set with-c is not the absolute number of CPU resources, but a relative weight value. The CPU resources that a container can eventually be allocated depend on its cpu share as a percentage of the total cpu share of all containers. Cpu share allows you to set the priority for containers to use CPU.

For example, two containers are launched in host:

Docker run-name "container_A"-c 1024 ubuntudocker run-name "container_B"-c 512 ubuntu

Container_A 's cpu share 1024 is twice that of container_B. When both containers require CPU resources, container_A can get twice as much CPU as container_B.

It is important to note that this weight-by-weight CPU only occurs when CPU resources are tight. If container_A is idle, container_B can also be allocated to all available CPU in order to make full use of CPU resources.

Container memory limit

Similar to the operating system, the memory available to the container consists of two parts: physical memory and swap. Docker controls container memory usage through the following two sets of parameters.

-m or-memory: sets the memory usage limit, such as 100m, 2G.

-memory-swap: sets the usage limit of memory + swap.

When we execute the following command:

Docker run-m 200m-- memory-swap=300M ubuntu

The implication is to allow the container to use up to 200m of memory and 100m of swap. By default, the above two sets of parameters are-1, which means there are no restrictions on the use of container memory and swap.

Let's use the progrium/stress image to allocate memory for the container. This image can be used to perform pressure tests on the container. Execute the following command:

Docker run-it-m 200m-memory-swap=300M progrium/stress-- vm 1-- vm-bytes 280m

-vm 1: starts a memory worker thread.

-vm-bytes 280m: each thread allocates 280m memory.

If the worker thread allocates more than 300m of memory, the allocated memory exceeds the limit, the stress thread reports an error, and the container exits.

If you specify only-m instead of-memory-swap when starting the container, then-memory-swap defaults to twice-m, for example:

Docker run-it-m 200m ubuntu

The container uses up to 200m physical memory and 200m swap.

That's all for "how to allocate memory and cpu to docker containers". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report