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/
ll
Since 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.0
Step 3: Replace the new container’s volume with the recovered one
Find the new container’s volume name:
docker inspect new_container_name
Stop the new container:
docker stop new_container_name
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 new_container_volume_name
# copy the recovered volume to the new container
cp -r recovered_volume_name new_container_volume_name
Start the container again:
docker start new_container_name
At this point the MySQL data is restored and the service is back online.
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.