0%

docker-compose 限制内存,CPU

docker-compose 使用3.7版本,然后加入 deploy,例如:

1
2
3
4
5
6
7
8
9
10
version: "3.7"
services:
redis:
image: redis:alpine
container_name: testredis
deploy:
resources:
limits:
cpus: '0.50'
memory: 500M

限制 CPU 使用率 50%, 500MB 内存

需要注意的是,启动时需增加 –compatibility 选项

1
docker-compose --compatibility up -d

否则会报错

1
WARNING: Some services (mysql, rsnmp) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm.

注:

–compatibility是docker-compose 1.20.0加入,主要目的就是用来将deploy中的数据限制、replicas与重启策略直接转译为version 2的语法

https://www.cnblogs.com/yjt1993/p/12402394.htm
https://www.jianshu.com/p/ba5518476057