Docker Compose 限制容器的CPU和内存

1. 内存和CPU限制

  • 编辑yml文件
    添加服务的 deploy内容如下:
    deploy:
      resources:
         limits:
            cpus: "2.00"
            memory: 5G
         reservations:
            memory: 200M
注意:reservations中不支持cpus,仅支持内存。

以ldap为例:

version: '3.7'
services:
  openldap:
    image: 10.10.239.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
  • 启动容器
# docker-compose --compatibility up -d
由于有资源限制, 且没有使用swarm, 所以要加上--compatibility参数, 否则报错
WARNING: Some services (web) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use docker stack deploy to deploy to a swarm.



引用自:jianshu.com/p/3ff5065bb

发布于 2022-02-23 10:29