Docker-compose限制容器CPU等
在docker-compose文件中使用deploy->resources->limits来限制容器使用的硬件资源。
yaml
version: '3'
services:
mysql:
image: mysql:8.0.27
container_name: mysql-test
restart: always
volumes:
- mysqlData:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: test
TZ: Asia/Shanghai
ports:
- "3306:3306"
deploy:
resources:
limits:
cpus: '0.50' # 容器最高使用50%的CPU
memory: 500M # 容器最高使用500M内存
volumes:
mysqlData:
启动的时候要加上--compatibility
参数:docker-compose --compatibility up -d
启动后,使用docker inspect mysql-test | grep 'Cpu\|Memory'
查看容器的CPU和内存:
bash
% docker inspect mysql-test | grep 'Cpu\|Memory'
"CpuShares": 0,
"Memory": 524288000, # 524288000 Byte/1024/1024 = 500 MB
"NanoCpus": 500000000, # 是满CPU的50%
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 1048576000,
"MemorySwappiness": null,
"CpuCount": 0,
"CpuPercent": 0,