Last week I was searching high and low for documentation of any kind on how to script a change in memory allocated to Docker Desktop for Windows. Unable to find anything online, and failing in all my attempts to piece together a way to make it happen, I opened an issue on GitHub and asked for advice. The fine folks in the Docker Desktop for Windows community on GitHub jumped in quickly to assist (Thank you again!). The suggestion was to change the Docker Desktop settings file directly. From that I was able to put together a process that works.
First we need to stop the docker services, there are two of them: com.docker.service and docker. To stop those is pretty straight forward.
Stop-Service com.docker.service Stop-Service docker
or shorter:
Stop-Service *docker*
Next we’ll need to read the settings file which is located @ ~\AppData\Roaming\Docker\settings.json. We’ll want to use the $env:APPDATA variable to get the actual path to the ~\AppData\Roaming folder on the current system for the current user, and we’ll pipe the file contents to ConvertFrom-Json to give us a nice object to work with.
$path = "$env:APPDATA\Docker\settings.json" $settings = Get-Content $path | ConvertFrom-Json
Now we can easily change the memory value by manipulating the memoryMIB property of our settings object.
$settings.memoryMiB = 4096
Then we can save the file again by piping our settings object to ConvertTo-Json and then to Set-Content.
$settings | ConvertTo-Json | Set-Content $path
Now we just need to restart the docker services.
Start-Service *docker*
There’s one last crucial step. It turns out we need to give the Docker daemon a little nudge to get things responding to our docker commands again. According to this article on stack-overflow we do that using:
& $Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchDaemon & $Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchDaemon
Yes, twice. I later derived by looking at the DockerCli help that I could just use:
&$Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchLinuxEngine
(I’m running Linux containers. If you are running Windows containers use -SwitchWindowsEngine instead.)
So here is the whole thing all in one go.
Stop-Service *docker* $path = "$env:APPDATA\Docker\settings.json" $settings = Get-Content $path | ConvertFrom-Json $settings.memoryMiB = 4096 $settings | ConvertTo-Json | Set-Content $path Start-Service *docker* &$Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchLinuxEngine