How to change the resources allocated to a contain

2020-02-26 09:01发布

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.

However, is there a way I can change these allocated resources to containers dynamically (after they have been provisioned) and without redeploying the same container with changed resources?

3条回答
戒情不戒烟
2楼-- · 2020-02-26 09:40

At the time (Docker v1.11.1) has the command docker update (view docs). With this you can change allocated resources on the fly.

Usage:  docker update [OPTIONS] CONTAINER [CONTAINER...]

Update configuration of one or more containers

  --blkio-weight          Block IO (relative weight), between 10 and 1000
  --cpu-shares            CPU shares (relative weight)
  --cpu-period            Limit CPU CFS (Completely Fair Scheduler) period
  --cpu-quota             Limit CPU CFS (Completely Fair Scheduler) quota
  --cpuset-cpus           CPUs in which to allow execution (0-3, 0,1)
  --cpuset-mems           MEMs in which to allow execution (0-3, 0,1)
  --help                  Print usage
  --kernel-memory         Kernel memory limit
  -m, --memory            Memory limit
  --memory-reservation    Memory soft limit
  --memory-swap           Swap limit equal to memory plus swap: '-1' to enable unlimited swap
  --restart               Restart policy to apply when a container exits
查看更多
3楼-- · 2020-02-26 09:51

not at present no - There is a desire to see someone implement it though: https://github.com/docker/docker/issues/6323

查看更多
The star\"
4楼-- · 2020-02-26 09:51

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)

查看更多
登录 后发表回答