How to restore docker container data

How to restore docker container data

The project test environment database data is lost, so I would like to record it. It was installed using Docker at the time, thinking it would be used temporarily for a period of time and not be persistent. Suddenly the day before yesterday, the docker log was full, and my colleague wanted to clean up the log and used the following command:

docker system prune

As a result, the MySQL container was normally in a stopped state at the time, but it was killed all of a sudden. The data we backed up was from March, which was terrible. Then various studies began to resume.

Then I went to the official documentation to study what this command does. docker system prune used above means:

Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.

Remove all unused containers, networks, images (dangling and unreferenced images), and optionally, volumes.

By default, volumes are not removed to prevent important data from being deleted if there is currently no container using the volume. Use the --volumes flag when running the command to prune volumes as well:

By default, if there are no containers currently using the volume, the volume will not be deleted to prevent deletion of important data. You can also use the --volumes flag when running the command to prune volumes:

Now I feel relieved. Fortunately, the data volume has not been deleted. We can use the data volume to recover the data. Next, I will record my recovery plan.

1. Find the data volume location

The data volume directory is under /var/lib/docker/volumes . Each container will have a folder under this directory. If the container still exists, we can use docker inspect 容器ID to view the data volume location. Now the container has been deleted, what can we do? We can only look for them one by one. Generally, there will be a _data directory under the MySQL container data volume directory, which will display the folders of each database, and finally found it.

image-20210510113543085

image-20210510114631505

This cxhello is our test library, now we can restore the data.

2. Recovery

Use docker volume create 數據卷名字command to create a new volume, and docker volume ls to view the list of volumes.

image-20210510114945865

Note: When using a data volume for mounting, the data volume must be an empty directory, that is, it cannot contain any data.

Then create the container

docker run -d -p 3309:3306 -v mysqldata:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name huifu mysql:5.7

image-20210510115714866

Before recovering data, you need to delete the associated content in the newly created data volume, and then copy the previous data volume content to the current data volume for data recovery.

cd /var/lib/docker/volumes/mysqldata/_data/
rm -f *
rm -f -R *

image-20210510130608341

Copy content to data volume

cd /var/lib/docker/volumes/1db16a9dfdf3442b117ebc2ec11df5df4db717cfd567c77fa0a49905a9652fa0/_data/
cp -R * /var/lib/docker/volumes/mysqldata/_data/

image-20210510131213523

At this point, the database data recovery is complete. Enter the restored container to view

image-20210510131728135

References

https://docs.docker.com/engine/reference/commandline/system_prune/

https://www.cnblogs.com/cheyunhua/p/13433400.html

This is the end of this article on how to recover docker container data. For more information about docker container data recovery, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Steps to restore code from a Docker container image
  • Detailed explanation of Docker data backup and recovery process
  • Detailed explanation of psql database backup and recovery in docker
  • Detailed explanation of backup, recovery and migration of containers in Docker
  • Detailed explanation of Docker private warehouse recovery example

<<:  A method of making carousel images with CSS3

>>:  Design Theory: Textual Expression and Usability

Recommend

RGBA alpha transparency conversion calculation table

Conversion between rgba and filter values ​​under...

How to design and optimize MySQL indexes

Table of contents What is an index? Leftmost pref...

MySQL performance optimization tips

MySQL Performance Optimization MySQL is widely us...

How to use Docker-compose to build an ELK cluster

All the orchestration files and configuration fil...

Solution to uninstalling Python and yum in CentOs system

Background of the accident: A few days ago, due t...

JS uses map to integrate double arrays

Table of contents Preface Simulating data Merged ...

Seven Principles of a Skilled Designer (2): Color Usage

<br />Previous article: Seven Principles of ...

MYSQL Operator Summary

Table of contents 1. Arithmetic operators 2. Comp...

Two ways to implement text stroke in CSS3 (summary)

question Recently I encountered a requirement to ...

CSS3 realizes text relief effect, engraving effect, flame text

To achieve this effect, you must first know a pro...