I use Windows 10 Pro 22H2 with docker desktop v4.37.1 to run Windows containers.
When I run a container: docker run --rm -it mcr.microsoft.com/dotnet/framework/runtime:4.8 powershell it only uses 2 of the 8 logical processors available at the host:
PS C:\Users\Administrator> docker run --rm -it mcr.microsoft.com/dotnet/framework/runtime:4.8 powershell
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.
PS C:\> (Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors
2
PS C:\> exit
PS C:\Users\Administrator> (Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors
8
I know I could use the --cpus option to get more CPUs in the docker:
PS C:\Users\Administrator> docker run --cpus 8 --rm -it mcr.microsoft.com/dotnet/framework/runtime:4.8 powershell
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.
PS C:\> (Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors
8
PS C:\> exit
PS C:\Users\Administrator> (Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors
8
I want to use this machine and others with different numbers of logical processors to run Jenkins pipelines using docker. Therefore it is not an option for me to use this flag because when running the docker command in the Jenkins pipeline, I cannot know how many CPUs are in the host that is running the pipelie:
My question is: How can I make docker desktop use by default all the CPUs available in the host?
In my docker desktop settings I have set Use the WSL 2 based engine (which I am not sure if it applies to windows containers or to this question).
Since you are using Windows containers, it doesn’t matter which backend (like WSL2) you chose for Linux containers. You probably see fewer CPUs because you are using the HyperV isolation and not the process isolation. Process isolation required the same Windows version in the container, but later they started to support different versions which I have not a lot of idea about how well it is supported. You can still try passing --isolation process to the docker run command. Otherwise you will get a small HyperV vm around the container to include a supported kernel too.
It looks like the --isolation process is what I need. However, when I try to run docker with this command I get the following error:
PS C:\Users\Administrator> docker run --rm -it --isolation process mcr.microsoft.com/windows:20H2 powershell
docker: Error response from daemon: hcs::CreateComputeSystem 250ff4ed08448a56ee1119e5eba4025d35e233b407443fde535ac5cd280d9ca1: The container operating system does not match the host operating system.
PS C:\Users\Administrator> docker run --rm -it --isolation process mcr.microsoft.com/dotnet/framework/runtime:4.8 powershell
docker: Error response from daemon: hcs::CreateComputeSystem 9c2b5b7cf5f9627cb651470f2984db86470eabe42fa2a41527ca2662c2c49c60: The container operating system does not match the host operating system.
My os version is:
PS C:\Users\Administrator> [System.Environment]::OSVersion.Version
Major Minor Build Revision
----- ----- ----- --------
10 0 19045 0
PS C:\Users\Administrator>
In cannot find any Windows image with the specific build in any of the Windows docker repositories (windows, windows-insider, windows-server, windows-servercore or windows-nanoserver).
Any idea on how to run with --isolation process?
This is the behaviour I remembered. Using Linux containers is easier and Microsoft is working on making it easier on Windows too. This part is not done by Docker. Linux containers were not invented by Docker either So they need the support from the OS. You can check the version compatibility matrix
And this is a note on that site
With the exception of WS2022 + Windows 11, Windows Server containers are blocked from starting when the build number between the container host and the container image are different. For example, when the container host is version 10.0.14393.* (Windows Server 2016) and you attempt to run a container with an image version 10.0.16299.* (Windows Server, version 1709) the OS compute service will return a version incompatibility error.
So the feature I mentioned before could be something available on Windows 11, but the versions still have to match on Windows 10.