rmc
rmc

Reputation: 1028

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?

Upvotes: 67

Views: 60622

Answers (6)

grinch
grinch

Reputation: 814

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

Upvotes: 3

VonC
VonC

Reputation: 1325137

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)

Upvotes: 3

Amos Folarin
Amos Folarin

Reputation: 2179

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)

Upvotes: 1

Thanh DK
Thanh DK

Reputation: 4397

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:

Upvotes: 58

Charles
Charles

Reputation: 590

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

Upvotes: 8

Pavel Nuzhdin
Pavel Nuzhdin

Reputation: 854

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.

Upvotes: 3

Related Questions