Limiting a Docker Container to a single cpu core

2019-03-09 19:40发布

I'm trying to build a system which runs pieces of code in consistent conditions, and one way I imagine this being possible is to run the various programs in docker containers with the same layout, reserving the same amount of memory, etc. However, I can't seem to figure out how to keep CPU usage consistent.

The closest thing I can seem to find are "cpu shares," which, if I understand the documentation, limit cpu usage with respect to what other containers/other processes are running on the system, and what's available on the system. They do not seem to be capable of limiting the container to an absolute amount of cpu usage.

Ideally, I'd like to set up docker containers that would be limited to using a single cpu core. Is this at all possible?

2条回答
对你真心纯属浪费
2楼-- · 2019-03-09 20:23

If you use a newer version of Docker, you can use --cpuset-cpus="" in docker run to specify the CPU cores you want to allocate:

docker run --cpuset-cpus="0" [...]

If you use an older version of Docker (< 0.9), which uses LXC as the default execution environment, you can use --lxc-conf to configure the allocated CPU cores:

docker run --lxc-conf="lxc.cgroup.cpuset.cpus = 0" [...]

In both of those cases, only the first CPU core will be available to the docker container. Both of these options are documented in the docker help.

查看更多
我想做一个坏孩纸
3楼-- · 2019-03-09 20:23

I've tried to provide a tutorial on container resource alloc.

https://gist.github.com/afolarin/15d12a476e40c173bf5f

查看更多
登录 后发表回答