How do I set resources allocated to a container using docker?

Docker

Docker Problem Overview


As the title of this question suggests I'm wanting to set max disk/memory and cpu usage for a container using docker (docker.io).

Is there a way to do this using just docker?

Docker Solutions


Solution 1 - Docker

Memory/CPU

Docker now supports more resource allocation options:

  • CPU shares, via -c flag
  • Memory limit, via -m flag
  • Specific CPU cores, via --cpuset flag

Have a look at docker run --help for more details.

If you use lxc backend (docker -d --exec-driver=lxc), more fine grained resource allocation schemes can be specified, e.g.:

docker run --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"\
           --lxc-conf="lxc.cgroup.cpu.shares = 1234"

Storage

Limiting storage is a bit trickier at the moment. Please refer to the following links for more details:

Solution 2 - Docker

You can now allocate a number of CPU shares to a container with the -c option as described here

Solution 3 - Docker

You can pass only memory limit (i.e. 5MB limit: docker run -m=5242880 ...image) as I know. But guys from docker.io planed to add CPU limits.

Solution 4 - Docker

Note: PR 15078 is implementing (Dec. 2015) support for changing resources both for stopped and running container (possibly docker 1.10 ou 1.11)

> We decided to allow to set what we called resources, which consists of cgroup thingies for now, hence the following PR #18073.
The only allowed mutable elements of a container are in HostConfig and precisely in Resources (see the struct).

resources := runconfig.Resources{
		BlkioWeight:       *flBlkioWeight,
		CpusetCpus:        *flCpusetCpus,
		CpusetMems:        *flCpusetMems,
		CPUShares:         *flCPUShares,
		Memory:            flMemory,
		MemoryReservation: memoryReservation,
		MemorySwap:        memorySwap,
		KernelMemory:      kernelMemory,
		CPUPeriod:         *flCPUPeriod,
		CPUQuota:          *flCPUQuota,
	}

> - The command should be set.

  • The allowed changes are passed as flags : e.g. --memory=1Gb --cpushare=… (as this PR does).
  • There is one flag for each attribute of the Resources struct (and no more, no less).

Note that making changes via docker set should persist.
I.e., those changes would be permanent (updated in the container's JSON)

Solution 5 - Docker

Just a note about -m / --memory --

If you are setting the memory limit but the container is not allocating the amount of memory you are trying to reserve, go into the preferences and adjust the memory being reserved to the docker app as a whole.

I ran into this 'problem' on OS X and wasn't sure why my container was being limited to ~2G when I was specifying --memory=8g

Solution 6 - Docker

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

  1. You an give a relative share of the cpus with --cpu-share='relative-number'

  2. you can now put hard limits on cpus:

    --cpuset="" specify which cpus by numeric id, 0=first, n=nth cpu. specify by contiguous "1-5" or discontiguous "1,3,5" ranges.

if using LXC instead of the default libcontainer then you can also specify this in:

--lxc-conf=[]              
(lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"

RAM:

-m, --memory=""            Memory limit (format: <number><optional unit>, where unit = b, k, m or g)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrmcView Question on Stackoverflow
Solution 1 - DockerThanh DKView Answer on Stackoverflow
Solution 2 - DockerCharlesView Answer on Stackoverflow
Solution 3 - DockerPavel NuzhdinView Answer on Stackoverflow
Solution 4 - DockerVonCView Answer on Stackoverflow
Solution 5 - DockergrinchView Answer on Stackoverflow
Solution 6 - DockerAmos FolarinView Answer on Stackoverflow