Customize resources
##Customizing resources available to individual container Windows containers can be restricted to utilize only certain amount of CPU/memory and other resources (file IO etc). Full list can be found here (https://docs.docker.com/engine/reference/run/#runtime-constraints-on-resources) Example below will show how this works on example of CPU utilization. During container startup you can specify CPU percentage throttling by modifying CPUPercent value of HostConfig object.
Import-module docker
$config = [Docker.DotNet.Models.Config]::new()
($config.ExposedPorts = [System.Collections.Generic.Dictionary[string,object]]::new()).Add("80/tcp", $null)
$hostConfig = [Docker.DotNet.Models.HostConfig]::new()
$pb = new-object Docker.DotNet.Models.PortBinding
$pb.HostPort = "8080"
$hostConfig.PortBindings = [System.Collections.Generic.Dictionary[string, System.Collections.Generic.iList[Docker.DotNet.Models.PortBinding]]]::new()
$hostConfig.PortBindings.Add("80/tcp",[System.Collections.Generic.List[Docker.DotNet.Models.PortBinding]]::new([Docker.DotNet.Models.PortBinding[]]@($pb)))
$hostconfig.CPUPercent = 33
Run-ContainerImage -ImageIdOrName "artisticcheese/iis:latest" -Configuration $config -Detach -HostConfiguration $hostConfig -Name $pb.HostPort
Once you launch the container issue following request to container http://<computerIP>:8080/pi/service.svc/pi/40000
this will bring CPU within container to 100. Issue several requests at the same time (since your container will x2 CPU by default and this will only load one CPU to 100).
To verify throttling happening, execute Get-Counter
cmdlet and point to virtual CPU which was assigned to your container by following this steps:
Get container ID of running container $ID = (get-container -Name 8080).ID
and then pull perfmon counter for virtual CPU 0 on running container. You can verify that CPU is in fact restricted to 33% set in Hostconfig
PS C:\Users\admin> get-counter "\Hyper-V Hypervisor Virtual Processor($($ID):Hv VP 0)\% Guest Run Time"
Timestamp CounterSamples
--------- --------------
1/20/2017 2:11:25 PM \\computer\hyper-v hypervisor virtual
processor(0eb0dfa74a611e2d122f84fa36e0387b034cd08e6f35a62cf20d22a2df28717a:hv vp 0)\% guest run
time :
32.8063423242168