How to change the resources allocated to a container at run time?

前端 未结 3 868
春和景丽
春和景丽 2020-12-16 02:17

I am aware that I can limit the resources allocated to a container while provisioning using docker with the -c and -m flags for CPU and memory.

3条回答
  •  我在风中等你
    2020-12-16 02:33

    That could be coming for docker 1.10 or 1.11 (Q1 2016): PR 15078 is implementing (Dec. 2015) support for changing resources (including CPU) both for stopped and running container.

    Update 2016: it is part of docker 1.10 and documented in docker update (PR 15078).

    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 (in the end: update).
    • 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)

提交回复
热议问题