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