How to use Docker to limit container resources

How to use Docker to limit container resources

Problem Peeping

In the server, assuming that the IIS service deploys multiple sites, as long as one of the sites has a problem, for example, the CPU is 100% or the memory is full, then the other sites on this server will also crash. Similarly, when using containers, dozens of containers may run on a single host. Although the containers are isolated from each other, they use the same kernel, CPU, memory, disk and other hardware resources as the host. If container resources are not restricted, containers will affect each other.

Solution

Docker provides methods to limit memory, CPU or disk IO, which can limit the size and amount of hardware resources occupied by the container. We can limit the hardware resources of this container when we use docker create to create a container or docker run to run a container.

Memory Limits

The memory limit functions provided by Docker are as follows:

1. The memory and swap partition size that the container can use.

2. The core memory size of the container.

3. Swapping behavior of container virtual memory.

4. Soft limit of container memory.

5. Whether to kill containers that occupy too much memory.

6. Container Killing Priority

-m, --memory Memory limit, the format is a number plus a unit, the unit can be b, k, m, g. Minimum 4M
--memory-swap Total limit of memory + swap partition size. Same format as above. Required -m sets the soft limit on memory. Same format as above --oom-kill-disable Whether to prevent OOM killer from killing containers, not set by default --oom-score-adj The priority of the container being killed by OOM killer, the range is [-1000, 1000], the default is 0
--memory-swappiness is used to set the virtual memory control behavior of the container. The value is an integer between 0 and 100. --kernel-memory Kernel memory limit. Same format as above, minimum size is 4M

The user memory limit is to limit the size of the memory and swap partition that the container can use.
There are two intuitive rules to follow when using it:

The minimum parameter of the -m, --memory option is 4M.
--memory-swap is not the swap partition, but the total size of the memory plus the swap partition, so --memory-swap must be larger than -m, --memory.

CPU Limit

All options related to the docker run command and CPU limit are as follows:

--cpuset-cpus="" The set of CPUs allowed to be used, the value can be 0-3,0,1
-c, --cpu-shares=0 CPU share weight (relative weight)
cpu-period=0 limits the CPU CFS period, ranging from 100ms to 1s, i.e. [1000, 1000000]
--cpu-quota=0 Limit CPU CFS quota, must be no less than 1ms, i.e. >= 1000
--cpuset-mems="" Allow execution on memory nodes (MEMs), only valid for NUMA systems

The --cpuset-cpus is used to set the vCPU cores that the container can use. -c, --cpu-shares is used to set the relative proportion of CPU time that can be allocated to each container when multiple containers compete for the CPU. --cpu-period and --cpu-quata are used to set the absolute amount of CPU time a container can use.

This is the end of this article about how to use docker to limit container resources. For more information about docker container resource restrictions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker and iptables and implementation of bridge mode network isolation and communication operations
  • Network management and network isolation implementation of Docker containers
  • How to isolate users in docker containers
  • Docker Modify Docker storage location Modify container image size limit operation
  • Implementation of Docker CPU Limit
  • How Docker limits the CPU available to containers
  • How to limit the memory available to a container in Docker
  • Introduction to Docker Isolation and Restriction Principles

<<:  Pure js to achieve the effect of carousel

>>:  Detailed explanation of CSS margin collapsing

Recommend

Vue implements simple comment function

This article shares the specific code of Vue to i...

Sample code of uniapp vue and nvue carousel components

The vue part is as follows: <template> <...

How to install and use Ubuntu Docker

Table of contents 1. Automatic installation using...

How to get the size of a Linux system directory using the du command

Anyone who has used the Linux system should know ...

Simple usage examples of MySQL custom functions

This article uses examples to illustrate the usag...

MySQL method steps to determine whether it is a subset

Table of contents 1. Problem 2. Solution Option 1...

How to use Nginx to proxy multiple application sites in Docker

Preface What is the role of an agent? - Multiple ...

How to quickly build ELK based on Docker

[Abstract] This article quickly builds a complete...

Detailed explanation of vue-router 4 usage examples

Table of contents 1. Install and create an instan...

CSS3 to achieve timeline effects

Recently, when I turned on my computer, I saw tha...

Summary of Commonly Used MySQL Commands in Linux Operating System

Here are some common MySQL commands for you: -- S...

Ubuntu starts the SSH service remote login operation

ssh-secure shell, provides secure remote login. W...

Share some key interview questions about MySQL index

Preface An index is a data structure that sorts o...