Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore deleted container docker

I have a docker container that was running postgresql db but it was accidentally deleted by my Apache Mesos Marathon.

Is there any way I can restore the container or at least, get the database files?

Thanks

like image 460
eugene Avatar asked Mar 23 '15 03:03

eugene


People also ask

Can you restore a deleted container from docker?

If you have a backup of your Docker Container stored as a Tar File in your local machine or pushed it on the Docker Hub account, you can restore that Tar File back into a Docker Image, create a Container and use it.

How do I recover a deleted docker image?

Go to /var/lib/docker/volumes/ . In this folder, there are folders which are named as container IDs. Each container ID folder has a _data folder that contains the application files for that container. Find the specific volume you wish to restore and save its container ID somewhere to remember it in the next steps.

What happens when container is deleted in docker?

Docker uses storage drivers to store image layers, and to store data in the writable layer of a container. The container's writable layer does not persist after the container is deleted, but is suitable for storing ephemeral data that is generated at runtime.

How do I restore docker?

Restore your dataUse docker pull to restore images you pushed to Docker Hub. If you backed up your images to a local tar file, use docker image load -i images. tar to restore previously saved images. Re-create your containers if needed, using docker run , or Docker Compose.


2 Answers

If it's just stopped, rather than removed, you should be able to find it under docker ps -a and just run docker start CONTAINER.

Unless the database was removed with docker rm -v CONTAINER, the database files are probably still in a directory somewhere under /var/lib/docker/vfs/dir/, but you may have a hard time figuring out which one. If you do manage to figure out the correct directory, you should be able to restore the db by just mounting the directory into a new database instance.

like image 184
Adrian Mouat Avatar answered Sep 18 '22 12:09

Adrian Mouat


The accepted answer is correct, however I would like to add the specific steps to restore an app (in my case it was a PostgreSQL database) via its docker volume.

I am using Docker version 18.09.0, build 4d60db4 in CentOS.

  1. Go to /var/lib/docker/volumes/.
  2. In this folder, there are folders which are named as container IDs. Each container ID folder has a _data folder that contains the application files for that container. Find the specific volume you wish to restore and save its container ID somewhere to remember it in the next steps.
  3. Run the command docker volume ls to make sure that container ID is listed as volume. That will be our volume_name to restore.
  4. If you don't remember the default mount destination, run a dummy container via the command docker run -d --name postgres -p 5432:5432 postgres.
  5. Run the command docker inspect postgres
  6. Find the Mounts section and check the Destination. In my case it was /lib/postgresql/data.
  7. Stop and remove the dummy container you just started.
  8. Run the command docker run -d --name postgres -p 5432:5432 --mount source=volume_name,target=/var/lib/postgresql/data postgres
  9. At this point a container that contains the old data will be available.
like image 27
Murat Aykanat Avatar answered Sep 20 '22 12:09

Murat Aykanat