Modify the image file storage location of docker on Windows (modify WSL file mapping)

1, cause

After installing docker on Windows at the beginning, it defaults to pull the image and save it to the ext4.vhdx file in C:\Users\<你当前用户>\AppData\Local\Docker\wsl\data\the path . When the pulled image becomes larger and larger, students with smaller C disks will not be able to hold it. How to modify the storage location of the docker image file on Windows becomes imminent.

2. Modify

Click to install Docker Desktop and start it. It will prompt you to install WSL. If you don’t install it, you won’t be able to start the docker engine service.

Because the current docker relies on WSL for file mapping, so we need to modify the path of docker's file mapping (which can be understood as file mounting) through wsl.

Now we are all installing WSL2, the docker-desktop-data disk image in WSL2 mode is usually located in the following location:C:\Users\<你当前用户名>\AppData\Local\Docker\wsl\data\ext4.vhdx

Follow the steps below to relocate it to a different drive or directory and keep all existing Docker data.

2.1 Close Docker Desktop

First, right-click the Docker Desktop icon in the lower right corner of the taskbar to close the Docker desktop, and choose to exit the Docker desktop. After a while, the Docker icon disappears, which proves that Docker is completely closed. Then, open the command prompt:

wsl --list -v

We'll be able to see, make sure both states are stopped.

insert image description here

By default, Docker Desktop for Window will create the following two distributions (distro)
C:\Users<your current user name>\AppData\Local\Docker\wsl
docker-desktop (corresponding to distro/ext4.vhdx)
docker-desktop -data (corresponding to data/ext4.vhdx)
According to the official instructions : the vhdx file supports a maximum of 256G, and there will be exceptions if the size is exceeded.

2.2 Backup image

Export docker-desktop-data to a file (backup image and related files), I save it to the D disk, use the following command, you can modify the path according to your needs:

wsl --export docker-desktop-data "D:\\docker-desktop-data.tar"

2.3 WSL unregister docker-desktop-data

Unregister docker-desktop-data from wsl, note that C:\Users\<你当前用户名>\AppData\Local\Docker\wsl\data\ext4.vhdxthe file will be deleted automatically, the command is as follows:

wsl --unregister docker-desktop-data

2.4 Import the image data back to WSL and mount it to the new path

Import the exported docker-desktop-data back to wsl, and set the path we want, that is, the new image and the mounting directory of various files used by docker. I set it to D:\docker\wsl here, and the command is as follows :

wsl --import docker-desktop-data "D:\\docker\\wsl" "D:\\docker-desktop-data.tar" --version 2

After the command is executed, you can see the files in the directory. At this time, start Docker Desktop for the first time and it can work normally.

3. Verification

Record the size of C:\Users\<你当前用户名>\AppData\Local\Docker\wslthe directory , and then look at the file size of the path after modification, you can see the change (provided that the image is pulled before modification, and the image is relatively large), and then docker pull any image (I downloaded Ubuntu ), and see that C:\Users\<你当前用户名>\AppData\Local\Docker\wslthe directory remains unchanged, but D:\\docker\\wsl(the mirror mount directory you modified) increases.

Finally, if there is no problem after verification, you can delete D:\docker-desktop-data.tar (the image file backed up earlier), remember that it is not the ext4.vhdx file, which is an important file. After deleting Docker Desktop, it will Can't open it.

4. Appendix

For the problem that WSL2 disk space is not released after Windows docker deletes the container, please refer to: WSL2 disk space is not released after Windows docker deletes the container

おすすめ

転載: blog.csdn.net/a6661314/article/details/128007347