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

匿名 (未验证) 提交于 2019-12-03 03:04: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 the services . But it fails while using version 3 , putting them under deploy section doesn't seem worthy unless i am using swarm mode .

Can somebody help ?

version: "3" services:   node:     build:      context: .       dockerfile: ./docker-build/Dockerfile.node     restart: always     environment:       - VIRTUAL_HOST=localhost     volumes:       - logs:/app/out/     expose:       - 8083     command: ["npm","start"]     cap_drop:       - NET_ADMIN       - SYS_ADMIN 

回答1:

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/You-Pre-Build-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 not necessary, will be exposed per default on your stack network.
  • Images have to be pre-built. Buil within v3 not possible
  • "Restart" is also decrapted. U 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:

ports:   - 80:80 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!