Recovering MySQL Data from a Deleted Docker Container
This guide walks through locating the Docker volume of a removed MySQL container, creating a new container with the same image, and swapping the volumes to restore the original database, providing step‑by‑step commands and screenshots for successful data recovery.
While testing MySQL gap locks, the author discovered a forgotten MySQL container on a server and, after removing it with docker rm -f, realized the database was still actively used by a project.
Step 1: Locate the volume of the deleted container
Navigate to Docker's volume directory and list the volumes:
cd /var/lib/docker/volumes/ llSince the database is MySQL, search for the mysql.ibd file to identify the relevant volume: find . -name mysql.ibd Three MySQL‑related volumes appear; by checking creation times and inspecting the directory contents, the correct volume is identified.
Step 2: Launch a new container with the same image
Start a new MySQL container using the same image version:
docker run -itd -p 3306:3306 -e MYSQL_ROOT_PASSWORD=xxxxxx mysql:8.0Step 3: Replace the new container’s volume with the recovered one
Find the new container’s volume name: docker inspect <em>new_container_name</em> Stop the new container: docker stop <em>new_container_name</em> Replace its volume with the one found in Step 1:
# go to Docker volumes directory cd /var/lib/docker/volumes/ # delete the new container’s volume rm -rf <em>new_container_volume_name</em> # copy the recovered volume to the new container cp -r <em>recovered_volume_name</em> <em>new_container_volume_name</em>Start the container again: docker start <em>new_container_name</em> At this point the MySQL data is restored and the service is back online.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
