Limiting the docker daemon CPU resource

Viewed 3099

When I do docker info it gives me the following result:

OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 2
Total Memory: 7.632 GiB

but I want the cpu to be 1 and not 2.How do I restrict the CPU for the docker itself?

3 Answers

On windows, open up powershell and type

# turn off all wsl instances such as docker-desktop
wsl --shutdown
notepad "$env:USERPROFILE/.wslconfig"

Edit .wslconfig file with notepad and write down these settings:

[wsl2]
memory=3GB   # Limits VM memory in WSL 2 up to 3GB
processors=2 # Makes the WSL 2 VM use two virtual processors

type wsl in powershell

restart docker

type docker info and you should see something like

Architecture: x86_64
CPUs: 2
Total Memory: 6.789GiB

Credits to Ali Bahraminezhad, check out his post here

As specified in the docker documentation you can limit the container resources usage by specifying the --cpus flag when running. The docker info command is returning the number of cpus on the machine.

To limit the container to use only one cpus use the following:

docker run --cpus="1" ...

Yo can limit resources to docker using docker machine. In other words you can limit resources assigned to virtual machine itself from your VM provider itself.

To create a new docker machine and specify its configurations:

docker-machine create -d virtualbox --virtualbox-memory=4096 --virtualbox-cpu-count=2 --virtualbox-disk-size=50000 nameOfYourMachine

To modify current one you can use VBoxManage that comes with Virtualbox, stop VM first, change settings and start it again.

docker-machine stop
VBoxManage modifyvm default --cpus 1
VBoxManage modifyvm default --memory 4096
docker-machine start

In Mac or windows where you have GUI, you can easily limit the docker resources CPU/memory from Preferences or Settings menu as shown below.

enter image description here

Related