How to specify Memory & CPU limit in docker compose version 3

前端 未结 3 1897
悲哀的现实
悲哀的现实 2020-11-30 21:01

I am unable to specify CPU & memory for services specified in version 3 .

With version 2 it works fine with \"mem_limit\" & \"cpu_shares\" parameters under t

3条回答
  •  北海茫月
    2020-11-30 21:37

    deploy:
      resources:
        limits:
          cpus: '0.001'
          memory: 50M
        reservations:
          cpus: '0.0001'
          memory: 20M
    

    More: https://docs.docker.com/compose/compose-file/#resources

    In you specific case:

    version: "3"
    services:
      node:
        image: USER/Your-Pre-Built-Image
        environment:
          - VIRTUAL_HOST=localhost
        volumes:
          - logs:/app/out/
        command: ["npm","start"]
        cap_drop:
          - NET_ADMIN
          - SYS_ADMIN
        deploy:
          resources:
            limits:
              cpus: '0.001'
              memory: 50M
            reservations:
              cpus: '0.0001'
              memory: 20M
    
    volumes:
      - logs
    
    networks:
      default:
        driver: overlay
    

    Note:

    • Expose is not necessary, it will be exposed per default on your stack network.
    • Images have to be pre-built. Build within v3 is not possible
    • "Restart" is also deprecated. You can use restart under deploy with on-failure action
    • You can use a standalone one node "swarm", v3 most improvements (if not all) are for swarm

    Also Note: Networks in Swarm mode do not bridge. If you would like to connect internally only, you have to attach to the network. You can 1) specify an external network within an other compose file, or have to create the network with --attachable parameter (docker network create -d overlay My-Network --attachable) Otherwise you have to publish the port like this:

    ports:
      - 80:80
    

提交回复
热议问题