I removed my mongo container
docker rm myMongoDB
Did I lose all my data, or I can restore it? If so, how?
When I try to run another container from the image
docker run -p 27017:27017 -d mongo --name myMongo2
it won't run and its STATUS says Exited (2) 8 seconds ago
.
The official mongo
image on Docker Hub (https://hub.docker.com/_/mongo/) defines two volumes to store data in the Dockerfile. If you did not explicitly specify a -v
/ --volume
option when running the container, Docker created anonymous (unnamed) volumes for those, and those volumes may still be around. It may be a bit difficult to find which volumes were last used by the container, because they don't have a name.
To list all volumes that are still present on the docker host, use;
docker volume ls
Which should give you something like this;
DRIVER VOLUME NAME
local 9142c58ad5ac6d6e40ccd84096605f5393bf44ab7b5fe51edfa23cd1f8e13e4b
local 4ac8e34c11ac7955b9b79af10c113b870edd0869889d1005ee17e98e7c6c05f1
local da0b4a7a00c4b60c492599dabe1dbc501113ae4b2dd1811527384a5dc26cab13
local 81a40483ae00d72dcfa2117b3ae40f3fe79038544253e60b85a8d0efc8f3d139
To see what's in a volume, you can attach it to a temporary container, and check what's in there. For example;
docker run -it -v 81a40483ae00d72dcfa2117b3ae40f3fe79038544253e60b85a8d0efc8f3d139:/volume-data ubuntu
That will start an interactive shell in a new ubuntu container, with the volume 81a40483ae00d72dcfa2117b3ae40f3fe79038544253e60b85a8d0efc8f3d139
mounted at /volume-data/
inside the container.
You can then go into that directory, and check if it's the volume you're looking for:
root@08c11a34ed44:/# cd /volume-data/
root@08c11a34ed44:/volume-data# ls -la
once you identified which volumes (according to the Dockerfile, the mongo image uses two), you can start a new mongo container, and mount those volumes;
docker run -d --name mymongo \
-v 4ac8e34c11ac7955b9b79af10c113b870edd0869889d1005ee17e98e7c6c05f1:/data/db/ \
-v da0b4a7a00c4b60c492599dabe1dbc501113ae4b2dd1811527384a5dc26cab13:/data/configdb/ \
mongo
I really suggest you read the Where to Store Data section in the documentation for the mongo image on Docker Hub to prevent loosing your data.
NOTE
I also noted that your last command puts the --name myMongo2
after the image name; it should be before mongo
(the image name). Also myMongo2
is an invalid container name, as it is not allowed to have uppercase characters.