使用 --cpus=<value>
命令,在 docker run
的时候进行分配,
If you have 1 CPU, each of the following commands guarantees the container at most 50% of the CPU every second.
Docker 1.13 and higher:
docker run -it --cpus=".5" ubuntu /bin/bash
Docker 1.12 and lower:
$ docker run -it --cpu-period=100000 --cpu-quota=50000 ubuntu /bin/bash
更多的参数可以参考:
Limit a container's resources
这是命令行参数,yml中怎么写?
园子里也有一篇相关博文:Docker: 限制容器可用的 CPU
@dudu:
In this general example, the redis service is constrained to use no more than 50M of memory and 0.50 (50%) of available processing time (CPU), and has 20M of memory and 0.25 CPU time reserved (as always available to it).
version: '3'
services:
redis:
image: redis:alpine
deploy:
resources:
limits:
cpus: '0.50'
memory: 50M
reservations:
cpus: '0.25'
memory: 20M
Reference: Compose file version 3 reference