How to allocate 50% CPU resource to docker container?

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 how to use the -c, --cpu-shares=0 option.

docker run -c 0.5 -i -t ubuntu /bin/bash


  • NodeJs development offline in docker
  • Create a network for two machines in docker that is not reachable from the host system
  • Docker Machine vs Docker run
  • Unable to build docker from root
  • Spark hangs on authentication with a Docker Mesos cluster
  • couchbase - [Errno 113] No route to host
  • docker run -c 2 -i -t ubuntu /bin/bash

  • Docker expose all ports or range of ports from 7000 to 8000
  • Does it make any difference to include or omit :latest when pulling a Docker image?
  • Java Spring Hibernate application runs on local, but not in aws's ecs docker
  • Is it possible to share memory between docker containers?
  • Linking Django and Postgresql with Docker
  • Cannot run nodejs app and mongo within a docker container
  • 4 Solutions collect form web for “How to allocate 50% CPU resource to docker container?”

    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).

    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

    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 /
    sudo docker run -c 410 -dit --name web nginx /

    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.

    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)

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

    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.


    $ 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:

    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

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.