SoFunction
Updated on 2025-03-09

Detailed explanation of the steps to modify Docker's default storage path

introduction

With the widespread use of Docker technology, it greatly simplifies the deployment and maintenance process of complex projects and can be easily run with a single image. However, as the amount of data continues to grow, Docker's default data storage method may gradually become a challenge, especially when installed by default in the root directory ("/"), which can quickly consume valuable system space. Faced with the dilemma of insufficient root directory space, especially when loading large images, it is particularly critical to adjust Docker's Root Dir to a dedicated mount data directory.

To effectively deal with this problem, migrating Docker's data storage to a specially configured storage location can not only free up the tight space of the root directory, but also optimize data management and scalability. By flexibly configuring Docker's storage path, users can ensure that the system remains efficient and stable even when processing large images or data sets.

1. Check the default directory (see whether the storage path is on the system disk)

docker info

Generally, the default directory is:

Docker Root Dir: /var/lib/docker

2. Modify the default directory

1. Make sure to stop the docker service

systemctl stop 

Some people may encounter the following error:

Warning: Stopping , but it can still be activated by:

Then execute it first 
 
systemctl stop 
 
Execute again
 
systemctl stop 

2. Create a new directory to store Docker data.

For example,Store data in /www/docker/:
mkdir -p /www/docker/

3. Copy the Docker data directory

Copy all data in the current /var/lib/docker directory to a new directory and use rsync instead of cp to solve the problem of breakpoint continuation:

rsync -avz /var/lib/docker/* /home/docker/

4. Modify the /etc/docker/ file, add and replace the directory address

By default, Docker uses the /etc/docker/ configuration file, and if this file is not available, it needs to be created manually.

touch /etc/docker/
vim /etc/docker/
Added the following information:
{
"data-root": "/www/docker"
}

"/www/docker" is my target address

Save and close the file

5. Start the docker service

systemctl start docker

or

According to the shutdown operation just now, start socket and sequence in turn

systemctl start 
systemctl start 

6. Verification

Check the value of Docker Root Dir again

docker info
df -h
docker ps
// Or directly verify whether the service is available

Check whether Docker Root Dir is modified to the specified directory

Delete the original directory again

cd /var/lib
rm -rf docker

Notice:

  • Before performing these steps, make sure you have backed up all important Docker data in case of accidental loss.
  • After moving data and changing configurations, be sure to perform tests to ensure everything works properly.

This is the article about the detailed explanation of the steps to modify Docker's default storage path. For more information about modifying Docker paths, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!