Since the C disk of the ThinkPad T480 used is a built-in 128G SSD disk and uses the Windows 10 operating system, with the installation of development tools, the C disk often warns that the storage space is insufficient, especially after installing Docker Desktop and performing various The image container test operation is even more difficult because the default storage location of Docker image containers is on the C drive.
In the case that the SSD disk cannot be quickly expanded, it will be a workaround to analyze the storage of the C disk related to Docker (using the WSL2 engine) and make relevant changes.
1. Default storage situation analysis and solutions
The storage related to Docker mainly has the following levels:
1) The container engine WSL2 default Distro Ubuntu
This is installed via the Windows Store, and its default storage location is:
file:///C:/Users/<UserName>/AppData/Local/Packages/CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc/LocalState/
Details can be accessed via: \\wsl$\Ubuntu
The change of the location of the Ubuntu installation package is not considered here:
C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu_2204.1.8.0_x64__79rhkp1fndgsc
This is a Windows application and takes up approximately: 570M.
2) Default installation location and redirection of Docker Desktop application files
The default installation location of the Windows Docker Desktop application is:
C:\Program Files\Docker
And there is no option when installing (as of version 2023/02/07), you can use the directory link to install the application to the D drive, etc., using the following command:
mklink /j "C:\Program Files\Docker" "D:\Program Files\Docker"
Download the Windows Docker Desktop application and install it directly.
As can be seen from the following figure, the C drive shows the form of the directory link:
The specific content is installed under the D drive:
It should be noted that in this installation mode, when the Docker Desktop application is automatically updated and upgraded, sometimes the application file will be physically installed on the C drive, and then find the time to relocate and solve it.
3) The storage location of Docker application's own system, user image files and container data files
By default, Docker-related images are stored in two directories under the C drive, and you can choose if they cannot be installed:
You can use the following wsl command together with 1) to rearrange the storage locations of relevant directories and files.
2. Changes in the location of Docker-related storage files
1) Stop the WSL2 engine and Windows Docker Desktop application
As mentioned above, Docker-related storage files are all stored in the C drive system disk by default. In order to change the location of the stored data files, you first need to exit the Docker application and stop the wsl engine
wsl --shutdown
2) Query relevant storage information (default in C drive) and determine a new storage directory
The associated data and Distro can be listed with the following command:
wsl -l -v -all
docker-desktop-data and docker-desktop respectively correspond to two vhdx hard disk image files:
C:\Users\<user name>\AppData\Local\Docker\data\ext.vhdx
C:\Users\<user name>\AppData\Local\Docker\distro\ext.vhdx
You can use the wsl export and import commands to change the storage location of related files.
Ubuntu is a distro of wsl2, and the new storage directory is changed to:
d:\wsl-distro directory.
Docker相关的镜像文件存储及数据存储,新的存储目录将分别更改存储到:
d:\Docker\wsl\distro
d:\Docker\wsl\data
3) 执行存储位置更动命令
wsl --export Ubuntu D:\wsl-distro\ubuntu.tar
wsl --unregister Ubuntu
wsl --import Ubuntu D:\wsl-distro D:\ws-distro\ubuntu.tar --version 2
可以看到:
原来存储的文件已不存在。
而新的存储所在已生效。
同样对Windows DockerDesktop的相关存储执行系列操作:
Export操作:
wsl --export docker-desktop D:\Docker\wsl\distro\docker-desktop.tar
wsl --export docker-desktop-data D:\Docker\wsl\data\docker-desktop-data.tar
Unregister操作:
wsl --unregister docker-desktop-data
wsl --unregister docker-desktop
Import操作:
wsl --import docker-desktop-data d:\Docker\wsl\data\ D:\Docker\wsl\data\docker-desktop-data.tar --version 2
wsl --import docker-desktop d:\Docker\wsl\distro\ D:\Docker\wsl\distro\docker-desktop.tar --version 2
如下图所示:
同样可以看到原存储位置所占用空间已释放:
Import后也反映在新的存储位置:
4) 设置Docker Engine的存储位置
如前所示,默认情况下, Docker环境下的其它相关镜像文件及容器文件,存储在:C:\ProgramData\Docker目录下,可以通过Windows Docker Destop中的Docker Enginer的配置文件进行更改,具体是JSON文件中增加如下一行:
"data-root":"d\\docker"
然后重启Windows Docker Desktop, 后续运行所需的镜像文件及容器文件讲会存储到新的位置,而非原来的C盘下。
通过以上的操作,将可以一定程度上缓解C盘空间有限的情况下想继续使用Windows Docker Desktop进行更多探索而捉襟见肘的情况,特此分享。