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

How to modify the "Browse" button of the html form to upload files

Copy code The code is as follows: <!DOCTYPE HT...

Example code for implementing transparent gradient effects with CSS

The title images on Zhihu Discovery columns are g...

How to create a simple column chart using Flex layout in css

The following is a bar chart using Flex layout: H...

Vue application example code based on axios request encapsulation

Table of contents What is axios? Axios request ty...

Quickly learn MySQL basics

Table of contents Understanding SQL Understanding...

jQuery uses the canvas tag to draw the verification code

The <canvas> element is designed for client...

MySQL 5.7.16 ZIP package installation and configuration tutorial

This article shares the installation and configur...

A detailed analysis and processing of MySQL alarms

Recently, a service has an alarm, which has made ...

Native JS to achieve image marquee effects

Today I will share with you a picture marquee eff...

Display ellipsis effect when table cell content exceeds (implementation code)

illustrate In front-end development, you often en...

Use of Linux sed command

1. Function Introduction sed (Stream EDitor) is a...

How to add java startup command to tomcat service

My first server program I'm currently learnin...

Detailed explanation of Django+Vue+Docker to build an interface testing platform

1. Two words at the beginning Hello everyone, my ...