Docker changes image storage location

In the process of using Docker, if we operate everything with the default settings, after a period of use, you should find that the root directory space of the system is getting less and less, until one day you find that the space is full, and finally It turned out that it was occupied by Docker. So at this time, we need to deal with this painful problem. In the traditional PC era, we may be confused, because as far as notebooks are concerned, where do you need to add a hard disk. If it is a desktop computer, you can add it directly, but you still have to buy one, so at this time, it is time to recommend a wave of cloud servers. Of course, you will say that my xx application or xx website is running on the cloud server, but this is not the same as what I want to say. What I want to say is to use the cloud server as the usual development and testing machine, so that we can There will be no problems such as insufficient disk and insufficient memory, because once encountering insufficient, then add it, if the hard disk is not enough 100G, then add it to 500G, if the memory 4G is not enough, then add it to 8G, if it is not enough, then you think How much do you want, try to satisfy you, it's just a matter of a few simple operations. Well, let’s go too far, and I’m not going to advertise here. Let’s take a look at how to deal with the problem when the cloud server encounters Docker and the root directory is full. The solution to the problem is the first step: adding a new hard drive, and the second step migrating the data. For cloud services, it is very easy, and I will describe it in detail below. ## Mounting a cloud disk It is very easy to add a disk to a cloud server. You can directly add the disk to the host from the control panel. General cloud service software supports the automatic identification of the host. The next step is the internal operation of the host. I use it here. It is CentOS7, so some specific commands in the steps are also executed in the corresponding environment: 1. Use **fdisk** partition Use **fdisk** to partition the data disk, and execute the command `fdisk /dev/vdb`. Follow the prompts in sequence: a. Enter **n** and press Enter. b. Type **p** and press Enter. c. Enter **1**, **3** and press Enter. d. After entering **wq**, press Enter to start creating the partition. 2. Use **parted** to partition the data disk using **parted** and execute the command `parted /dev/vdb`. According to the prompts, do the following in sequence: a. Enter `mklabel gpt`, if prompted, enter **Y** and press Enter. b. `mkpart primary 0 -1`, if prompted, enter **I** and press Enter. c. Type `quit`, press Enter, and exit `parted`. 3. Format the new partition. Assuming the format is **"ext4"**, enter the command ``` $ mkfs.ext4 /dev/sdb1 ``` The format time varies according to the size of the cloud disk. The larger the cloud disk, the longer the formatting time. long. 4. Mount the disk ``` $ echo '/dev/vdb1 /mnt ext4 defaults 0 0' >> /etc/fstab $ mount -a $ df -h ``` At this time, you can see the newly mounted disk size and location, at this time, you can start transferring Docker data. ## Modify the location of Docker: By default, the storage location of docker is: `/var/lib/docker`. Generally, we will not give too much partition under the root. So at this time, we need to consider mounting the large partition to `/var/lib/docker`. The specific operation steps are as follows: 1. Stop Docker 2. Copy the Docker file to the directory `cp -r /var/lib/docker/* /mnt/` 3. Delete the original Docker directory `rm -rf /var/lib/docker` 4. Mount the partition as the Docker directory: `mount /dev/vdb1 /var/lib/docker` 5. Start Docker, use ` docker ps` and `docker images` confirm that the migration was successful ext4 /dev/sdb1 ```` The formatting time varies according to the size of the cloud disk. The larger the cloud disk, the longer the formatting time. 4. Mount the disk ``` $ echo '/dev/vdb1 /mnt ext4 defaults 0 0' >> /etc/fstab $ mount -a $ df -h ``` At this time, you can see the newly mounted disk size and location, at this time, you can start transferring Docker data. ## Modify the location of Docker: By default, the storage location of docker is: `/var/lib/docker`. Generally, we will not give too much partition under the root. So at this time, we need to consider mounting the large partition to `/var/lib/docker`. The specific operation steps are as follows: 1. Stop Docker 2. Copy the Docker file to the directory `cp -r /var/lib/docker/* /mnt/` 3. Delete the original Docker directory `rm -rf /var/lib/docker` 4. Mount the partition as the Docker directory: `mount /dev/vdb1 /var/lib/docker` 5. Start Docker, use ` docker ps` and `docker images` confirm that the migration was successful ext4 /dev/sdb1 ```` The formatting time varies according to the size of the cloud disk. The larger the cloud disk, the longer the formatting time. 4. Mount the disk ``` $ echo '/dev/vdb1 /mnt ext4 defaults 0 0' >> /etc/fstab $ mount -a $ df -h ``` At this time, you can see the newly mounted disk size and location, at this time, you can start transferring Docker data. ## Modify the location of Docker: By default, the storage location of docker is: `/var/lib/docker`. Generally, we will not give too much partition under the root. So at this time, we need to consider mounting the large partition to `/var/lib/docker`. The specific operation steps are as follows: 1. Stop Docker 2. Copy the Docker file to the directory `cp -r /var/lib/docker/* /mnt/` 3. Delete the original Docker directory `rm -rf /var/lib/docker` 4. Mount the partition as the Docker directory: `mount /dev/vdb1 /var/lib/docker` 5. Start Docker, use ` docker ps` and `docker images` confirm that the migration was successful

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325172053&siteId=291194637