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 limit the use of memory by Docker containers

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

How to limit the memory use of Docker containers? for this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Several containers run on a docker host, each of which requires CPU, memory, and IO resources. For virtualization technologies such as KVM,VMware, users can control how much CPU and memory resources are allocated to each virtual machine. For containers, Docker provides a similar mechanism to prevent one container from consuming too much resources to affect the performance of other containers and even the entire host.

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

Next we will use the progrium/stress image to learn how to allocate memory to 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 of memory.

Because 280m is within the assignable range (300m), the worker thread can work properly, and the process is

Allocate 280m of memory.

Free 280m of memory.

Re-allocate 280m of memory.

Release another 280m of memory.

It circulates all the time.

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 the-m, for example:

Docker run-it-m 200m ubuntu

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

This is the answer to the question on how to limit the use of memory by Docker containers. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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: 272

*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