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

后端 未结 5 1934
感情败类
感情败类 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: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.

提交回复
热议问题