Changing Docker for Windows' default storage directory for containers and images

Windows containers only

2019 update : Doing this via dockerd

This shows how we can set D:\ProgramData\docker as our directory rather than the default C:\ProgramData\docker by utilizing dockerd CLI's --data-root option which sets the "Root directory of persistent Docker state". See their API for further options

sc stop docker
cd "C:\Program Files\Docker\Docker\Resources"
.\dockerd.exe --unregister-service
.\dockerd.exe --register-service --data-root "D:\ProgramData\docker"
sc start docker
docker info

2018 way : No longer working

Windows containers and images are located in a folder located in C:\ProgramData\Docker on the O/S drive. As this directory can grow in size quite substantially, especially with Windows containers/images, I wanted to move this drive to a different location to avoid filling up the limited space within my O/S drive.

To begin, let's create a target directory for new location e.g. D:\ProgramData\Docker

mkdir D:\ProgramData\Docker

Open Docker For Windows > Settings > Daemon > Enable Advanced Mode > then modify the json setting with escaped slashes. This will restart the Docker daemon.

{
  "registry-mirrors": [],
  "insecure-registries": [],
  "debug": true,
  "experimental": true,
  "graph": "D:\\ProgramData\\Docker"
}

To verify whether this was successful use the docker info command

docker info

Then look for the Docker Root Dir setting

...
Docker Root Dir: D:\ProgramData\Docker
...

Linux containers only

2019 Update

Seems like you can now do this via Docker for Windows > Settings > Advanced > Disk Image Location which seems much easier

Short Version

  1. Shut down Docker for Windows
  2. Open Hyper-V, right click MobyLinuxVM, Move to "D:\Hyper-V\Virtual Hard Disks"
  3. Open %APPDATA%\Docker\settings.json, set MobyVhdPathOverride to "D:\\Hyper-V\\Virtual Hard Disks\\Virtual Hard Disks\\MobyLinuxVM.vhdx". (Remember to escape the slash)
  4. Start Docker for Windows

Long Version

Linux containers and images are stored in the
/var/lib/docker directory inside a VM named MobyLinuxVM hosted by Hyper-V which gets automatically created when you install Docker for Windows.

Checking the docker info

...
Docker Root Dir: /var/lib/docker
...

The VM's disk and other meta data is located in C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\

Say we want to move this VM into a directory to our D:\ drive we create a directory say D:\Hyper-V\Virtual Hard Disks

mkdir "D:\Hyper-V\Virtual Hard Disks"

Then open Hyper-V (virtmgmt.msc)

virtmgmt.msc > Virtual Machines > MobyLinuxVM > Actions > Move > Move all of the virtual machine's data to a single location > New Location > D:\Hyper-V\Virtual Hard Disks

But wait..this doesn't work yet because when we restart Docker for Windows, Docker will look for a VM named MobyLinuxVM Hyper-V's default directories and if it isn't there it will recreate an brand new VM with the same name in default directory - C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\. Not what we want!

Let's change that by going to %APPDATA%\Docker and opening settings.json (back this up first) and modify the MobyVhdPathOverride property and change null to "D:\\Hyper-V\\Virtual Hard Disks\\MobyLinuxVM.vhdx"

%APPDATA%\Docker\settings.json

{ 
  "LCOWExperimentalSwitchProposed": false,
  "LinuxDaemonOptionsCreationDate": "03/08/2018 23:14:49",
  "MobyVhdPathOverride": "D:\\Hyper-V\\Virtual Hard Disks\\MobyLinuxVM.vhdx",
  "NameServer": "8.8.8.8",
  "ProxyExclude": "",
  "ProxyHttp": "",
  "ProxyHttps": "",
  "SharedDrives": {},
  "StartAtLogin": true,
  "SubnetAddress": "10.0.75.0",
  "SubnetMaskSize": 24,
  "VmCpus": 2,
  "VmMemory": 2048,
  "WindowsDaemonOptions": null,
  "WindowsDaemonOptionsCreationDate": "03/09/2018 01:08:31",
  "WindowsDaemonOptionsV2": null
}