docker-compose限制容器cpu和内存
1. docker-compse中的语法
docker-compose.yml文件中添加 service.deploy
字段内容如下:
deploy:
resources:
limits:
cpus: "2.00"
memory: 5G
reservations:
memory: 200M
注意:
reservations
中不支持cpus
,仅支持内存。
2.以ldap为例说明
version: '3.7'
services:
openldap:
image: 10.10.xxx.54/public/openldap:1.3.0
container_name: openldap
environment:
- N9E_NID=22
ports:
- "389:389"
- "636:636"
deploy:
resources:
limits:
cpus: "2.00"
memory: 5G
reservations:
memory: 200M
volumes:
- ./ldap:/var/lib/ldap
- ./slapd.d:/etc/ldap/slapd.d
restart: always
- 启动容器
由于有资源限制, 且没有使用swarm, 所以要加上
--compatibility
参数, 否则报错
# docker-compose --compatibility up -d