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

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?

Answers


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:


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


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.


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


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)


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)

Need Your Help

Could not calculate build plan: Plugin org.apache.maven.plugins:maven-jar-plugin:2.3.2 or one of its dependencies could not be resolved

maven eclipse-plugin

I got the following error message when I tried to create a Maven project in eclipse. Many have posted about proxies in settings.xml file and also flush the .m2 folder forcing it to download a new o...

System.BadImageFormatException caused by NUnit project

c# 64bit nunit badimageformatexception

Good day everyone. I have been having the same problem all day at work and am struggling to find any new paths to go down.