How do I set resources allocated to a container us

2019-01-30 01:28发布

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
6条回答
smile是对你的礼貌
2楼-- · 2019-01-30 01:45

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)

查看更多
闹够了就滚
3楼-- · 2019-01-30 01:51

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:

查看更多
欢心
4楼-- · 2019-01-30 02:06

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

查看更多
萌系小妹纸
5楼-- · 2019-01-30 02:06

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)
查看更多
Animai°情兽
6楼-- · 2019-01-30 02:08

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

查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-01-30 02:09

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.

查看更多
登录 后发表回答