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

Several ways to encapsulate breadcrumb function components in Vue3

Table of contents Preface 1. Why do we need bread...

The button has a gray border that is ugly. How to remove it?

I used the dialog in closure and drew a dialog wit...

Vue+thinkphp5.1+axios to realize file upload

This article shares with you how to use thinkphp5...

Detailed explanation of nginx anti-hotlink and anti-crawler configuration

Create a new configuration file (for example, go ...

HTTP Status Codes

This status code provides information about the s...

How to quickly build your own server detailed tutorial (Java environment)

1. Purchase of Server 1. I chose Alibaba Cloud...

Research on the effect of page sidebar realized by JS

Table of contents Discover: Application of displa...

Super detailed teaching on how to upgrade the version of MySQL

Table of contents 1. Introduction 2. Back up the ...

MySQL 8.0.21 free installation version configuration method graphic tutorial

Six steps to install MySQL (only the installation...

Alibaba Cloud Server Tomcat cannot be accessed

Table of contents 1. Introduction 2. Solution 2.1...

Nginx reverse proxy forwards port 80 requests to 8080

Let's first understand a wave of concepts, wh...

A brief analysis of MySQL explicit type conversion

CAST function In the previous article, we mention...

Building an image server with FastDFS under Linux

Table of contents Server Planning 1. Install syst...

CentOS6.9+Mysql5.7.18 source code installation detailed tutorial

CentOS6.9+Mysql5.7.18 source code installation, t...