Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto persist MongoDB - data of a running Docker container in a new image?

Tags:

docker

mongodb

i have a running Mongo DB Container called xyz from offical Mongo Image. i created the container with docker run -d -p 21707:21707 mongo In this container i created 2 collections with sample data.

Now i want to extract this container in a new image on dockerhub.

I used docker commit and created a new image, pushed it on the docker hub. If i pull the image on a other system and create a new container from this image, there are no data like in my origin container xyz.

After research i found out that the mongo image could used with a volume, but i missed this step in the past... I think the container use /data/db as standard volume(docker inspect), on commit this volume is not attached to the new image?!

Also i tried docker export/import with the same problem mentioned above!

Now my Question, how could i reach to migrate this "xyz" running container with my sample data in a new image/container? Thanks a lot!

like image 861
immae Avatar asked Jan 12 '16 21:01

immae


People also ask

How do I persist data in Docker image?

Volumes are the best way to persist data in Docker. Bind mounts may be stored anywhere on the host system. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can modify them at any time.

How do I persist data in MongoDB?

Fortunately, MongoDB provides the write concern to accomplish the data persistence. You can set the write concern to “{j: true}” to make MongoDB acknowledge clients after the data is stored on the disk. Or, you can use “{w: majority}”, this implies “{j: true}” as well as replicates data to most slaves.

How can I access a MongoDB container from another container?

If you need to access the MongoDB server from another application running locally, you will need to expose a port using the -p argument. Using this method, you will be able to connect to your MongoDB instance on mongodb://localhost:27017 . You can try it with Compass, MongoDB's GUI to visualize and analyze your data.

How does MongoDB run in Docker container from host?

You can connect to MongoDB on localhost:27017 . Then use the following command to open the MongoDB shell. I have used mymongo as an arbitrary container name, though you can replace mymongo with test-mongo or any other container name of your choosing. The show dbs command will display all your existing databases.


1 Answers

I used docker commit and created a new image, pushed it on the docker hub. If i pull the image on a other system and create a new container from this image, there are no data like in my origin container xyz.

The mongo docker image writes its data into a volume. As a result, those data won't be saved to a new docker image created with docker commit. docker export won't produce your data for the same reason.

how could i reach to migrate this "xyz" running container with my sample data in a new image/container?

What you want is either:

  • create a new container re-using the first container's volume → see --volumes-from
  • save the first container's volume data into a directory on your docker host, and create a new container mounting this directory into the container → the docker run -v option

Also, this SO question might help you figure out volumes.

like image 198
Thomasleveil Avatar answered Oct 18 '22 20:10

Thomasleveil