Docker Desktop changes default storage path

Original link: https://www.bytecho.net/archives/2324.html

Preface

Recently I have started a lot of projects, mostly deployed with Docker, so I installed Docker on my computer before. However, after installing Docker Desktop on Windows, Docker saves the image to C:\Users\<用户>\AppData\Local\Docker\wsl\data\ by default. Under the ext4.vhdx file in C:\Users\<用户>\AppData\Local\Docker\wsl\data\ path, it is a waste to put these irrelevant files on the C drive, so I sorted out the method and moved these files to other disks.

Modify WSL

After installing Docker Desktop, you will be prompted to install WSL. If you do not install it, the Docker Engine service cannot be started. Because the current docker relies on WSL for file mapping, you can move these files to other disks by modifying docker’s file mapping path through wsl.

By default, Docker Desktop for Window creates the following two distributions:

  1. docker-desktop (distro/ext4.vhdx)
  2. docker-desktop-data (data/ext4.vhdx)

At present, WSL2 has been fully released. Docker-desktop-data under WSL2 is usually located in the following location: C:\Users\<你当前用户名>\AppData\Local\Docker\wsl\data\ext4.vhdx , this folder in my computer Up to 50GB+ , a complete waste of C drive space!

Before starting the operation, please exit Docker Desktop, then enter wsl --list -v in the terminal to ensure that both services are stopped:

image.png

Backup image

Enter the following commands to back up WSL respectively. The subsequent backup path can be modified by yourself. I backed up to the G disk here:

 wsl --export docker-desktop G:\docker-desktop.tar wsl --export docker-desktop-data G:\docker-desktop-data.tar

Unregister

 wsl --unregister docker-desktop wsl --unregister docker-desktop-data

Backup import

Before executing the command, please change G:\\docker-desktop.tar in the command. G:\\docker-desktop-data.tar is the path you backed up before, and the mounting path G:\\docker\\desktop , G:\\docker\\data also needs to be changed to the path you want to mount (the corresponding folder needs to be created in advance, otherwise it will prompt that the directory cannot be found):

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

test

Enter wsl --list -v to check whether the output is the same as before modification. Under normal circumstances, two releases will appear, namely: docker-desktop and docker-desktop-data .

Then start your Docker Desktop and see if it runs normally. Images created or pulled later will be stored in a new directory instead of the default path in the C drive.

Docker clear cache

Finally, let’s move on to a tutorial on cleaning up the docker cache~

When using docker build to build an image, Docker will gradually generate the Docker image according to the steps defined in the Dockerfile. During the image generation process, the results generated at each step will be cached so that the next time the image is generated, the same step does not have to be performed again to increase the speed of image building.

Use –no-cache

 docker build --no-cache .

Use docker system prune

Use docker system prune command to clean up resources that are no longer in use, including stopped containers, unmarked images, unused networks, and unused data volumes.

 # 清理所有不再使用的资源docker system prune # 清理更加彻底,将未使用Docker 镜像都删掉docker system prune -a

Henry 23-09-16

This article is reproduced from: https://www.bytecho.net/archives/2324.html
This site is only for collection, and the copyright belongs to the original author.