shrayus gupta
shrayus gupta

Reputation: 85

How to allocate percentage of system resources to Docker?

I know you can do something like

docker build -c 2 . 

to give the container 2 cores, but can you do something like give the container 50% of the memory and 50% of the CPU?

Upvotes: 3

Views: 1107

Answers (1)

Adrian Mouat
Adrian Mouat

Reputation: 46500

Your example docker build -c 2 ., doesn't actually do what you think it does. The -c flag assigns cpu-shares, which is a relative weighting with a default of 1024. So if another container is running with the default weighting and CPU usage is maxed out, your build container will only get 2/1026 of the CPU. If you want to use this mechanism to allocate CPU, you will need to do some maths based on the number of running containers and their existing weightings (e.g if there are two containers running with the default weighting, and you give a 3rd container a weighting of 2048, it will get 2048/(2048+1024+1024) or 50% of the CPU).

You can also use the --cpuset-cpus argument to control which cores the container runs on, which I think is what you're thinking of, but that will only help you if set it for all containers.

I think what you're actually after is the --cpu-quota setting which will use the Completely Fair Scheduler in the linux kernel. The period should be set to 100000 (100m)s by default, meaning the argument --cpu-quota=50000 should give the container 50% of 1 CPU.

Regarding memory, you can only set a maximum usage for each container, you can't allocate a percentage slice.

For full details on all of this, see https://docs.docker.com/reference/run/#runtime-constraints-on-resources

Upvotes: 5

Related Questions