Method of modifying the default storage location of Docker image of solution

  • 2021-10-27 09:50:02
  • OfStack

Due to the initial partition of the system, the corresponding/partition in the operating system will not be too large, and the pass/var directory will not be partitioned separately. If the Docker service is running above, it will make the already large partition less and less used after long-term use. How to deal with this problem better?

Step 1 Use soft links

We know that in the operating system, the default location of Docker container is under the/var/lib/docker directory. You can see the specific location by the following command.


#  Default storage location   
$ sudo docker info | grep "Docker Root Dir" 

The most direct and effective way to solve the problem of insufficient default storage capacity is to mount a new partition into this directory. However, in the case of the original system space is unchanged, so the way of soft link is adopted to modify the storage path of mirror and container to achieve the same purpose.


#  Stop Docker Services   
$ systemctl restart docker  
#  Stop Docker Services   
$ service docker stop 

Then move the entire/var/lib/docker directory to a destination path with less space. When you start Docker, you find that the storage directory is still the/var/lib/docker directory, but it is actually stored on the data disk/data/docker.


#  Move the original content   
$ mv /var/lib/docker /data/docker  
#  Link   
$ ln -sf /data/docker /var/lib/docker 

2. Specify container startup parameters

Specify the container startup parameter in the configuration file-graph=/var/lib/docker to specify the image and container storage path. The configuration file of Docker can set most of the background process parameters, and the storage location in each operating system is different. The location in Ubuntu is/etc/default/docker file, and the location in CentOS is/etc/sysconfig/docker file.


# CentOS6  
#  Because Ubuntu It is turned on by default selinux Mechanism   
OPTIONS=--graph="/data/docker" --selinux-enabled -H fd://  
# CentOS7  
#  Modify docker.service File, using -g Parameter specifies the storage location   
$ vi /usr/lib/systemd/system/docker.service  
ExecStart=/usr/bin/dockerd --graph /new-path/docker 
# Ubuntu  
#  Because Ubuntu Not turned on by default selinux Mechanism   
OPTIONS=--graph="/data/docker" -H fd:// 

After rebooting, the path of Docker is changed to/data/docker.


#  Re- reload Configuration file   
$ sudo systemctl daemon-reload  
#  Restart docker Services   
$ sudo systemctl restart docker.service 

If the version of Docker is 1.12 or above, you can modify or create a new daemon. json file. The modification will take effect immediately, and there is no need to restart Docker service.


#  Modify the configuration file   
$ vim /etc/docker/daemon.json  
{  
    "registry-mirrors":  
        ["http://7e61f7f9.m.daocloud.io"],  
    "graph": "/new-path/docker"  
} 

3. Create configuration file under System

Create an Drop-In file under the/etc/systemd/system/docker. service. d directory. The default docker. service. d folder does not exist and must be created first. The reason for creating the Drop-In file is that we want the Docker service to override the parameters used by the default service in the/lib/systemd/system/docker. service file with the specific parameters mentioned in the docker. conf file.


#  Define a new storage location   
$ sudo vi /etc/systemd/system/docker.service.d/docker.conf  
[Service]  
ExecStart=/usr/bin/dockerd --graph="/data/docker" --storage-driver=devicemapper 

Save and exit the vim editor/data/docker is the new storage location, and devicemapper is the storage driver currently used by Docker. If your storage driver is different, please enter the value viewed and noted in Step 1 before. Now, you can reload the service daemon and start the Docker service, which will change the storage location of the new image and container. To confirm that the 1 cut goes well, run the docker info command to check the root directory of Docker.


#  Re- reload Configuration file   
$ sudo systemctl daemon-reload  
#  Restart docker Services   
$ sudo systemctl start docker  

Related articles: