How to allocate 50% CPU resource to docker container via `docker run`?

后端 未结 5 1918
感情败类
感情败类 2020-12-13 19:10

I have a 4-core CPU, I want to allocate 50% CPU resource to a docker container.
After reading the docker-run manual and config.go source code.
I still don\'t know ho

相关标签:
5条回答
  • 2020-12-13 19:48

    Note: PR 15078 is implementing (Dec. 2015) support for changing resources (including CPU) 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)


    Check also the latest (July 2019, 4 years later) openJDK 8u212 (or later) which does have official Docker support in it.

    0 讨论(0)
  • 2020-12-13 19:49

    Since Docker 1.13, in your 4-core machine just add docker container run --cpus 2.0 [args...].

    Explanation from this blog post:

    In 1.13 though, if you want a container to be limited to one cpu then you can just add --cpus 1.0 to your Docker run/create command line. If you would like two and a half cpus as the limit of the container then just add --cpus 2.5. In Docker we are using the CFS quota and period to limit the container’s cpu usage to what you want and doing the calculations for you.

    Also check the docs.

    0 讨论(0)
  • 2020-12-13 19:51

    cpu-shares is a 'relative weight', relative to the default setting of 1024, so if you had two containers running on the same core, you could give them the CPU 50-50 or 80-20 or whatever you wanted by adjusting the numbers. It is an integer.

    You cannot give an overall limit, as you want to, using this flag, but you can restrict the set of CPUs that the container runs on using --cpuset mentioned here.

    The number 1024 is in the Cgroups docs.

    This blog post from Marek Goldmann explains resource management in Docker.

    See also Setting absolute limits on CPU for Docker containers, which says it can be done with lxc (older Docker implementation) but not libcontainer (current Docker implementation).

    0 讨论(0)
  • 2020-12-13 19:57

    It depends on the environment, so there is no straight answer but keep reading.

    From the docker run --help command:

    -c, --cpu-shares=0         CPU shares (relative weight)
    

    Since Docker is based on cgroups. The CPU will be distributed among the running containers. By default the value is 1024.

    cat /sys/fs/cgroup/cpu/docker/cpu.shares
    1024
    



    So, if we have 2 containers, one for the database and one more for the web server

    sudo docker run -c 614 -dit --name db postgres /postgres.sh
    sudo docker run -c 410 -dit --name web nginx /nginx.sh
    

    Will give 60% to the db container (614 is 60% of 1024) and 40% to the web container.



    For further reading see:

    • cpu shares cgroups documentation.
    • The cpuset option: --cpuset="" CPUs in which to allow execution (0-3, 0,1)
    • Resource management in Docker post.
    • Chapter 3 Configuring Docker Containers of the book Orchestating Docker by Shrikrishna Holla.
    0 讨论(0)
  • 2020-12-13 20:00

    Take a look here, this is apparently what you were looking for:

    https://docs.docker.com/engine/reference/run/#cpu-period-constraint

    The default CPU CFS (Completely Fair Scheduler) period is 100ms. We can use --cpu-period to set the period of CPUs to limit the container’s CPU usage. And usually --cpu-period should work with --cpu-quota.

    Examples:

    $ docker run -it --cpu-period=50000 --cpu-quota=25000 ubuntu:14.04 /bin/bash
    

    If there is 1 CPU, this means the container can get 50% CPU worth of run-time every 50ms.

    period and quota definition:

    Within each given "period" (microseconds), a group is allowed to consume only up to "quota" microseconds of CPU time. When the CPU bandwidth consumption of a group exceeds this limit (for that period), the tasks belonging to its hierarchy will be throttled and are not allowed to run again until the next period.

    0 讨论(0)
提交回复
热议问题