9

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!

immae
  • 123
  • 1
  • 2
  • 5
  • PS. i know that i could use "mongoexport" and then restore with "mongoimport" but the problem is, i have to create new users in the mongodb too... – immae Jan 12 '16 at 21:09

2 Answers2

7

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.

Community
  • 1
  • 1
Thomasleveil
  • 95,867
  • 15
  • 119
  • 113
  • i tried this: 1.docker run --volumes-from 6213f69189bc -v $(pwd):/backup node tar cvf /backup/backup.tar /data/db 2. i got a backup.tar in my folder 3. create a new container ddec84b89544 from my docker hub image. 4. docker run --volumes-from ddec84b89544 -v $(pwd):/backup ubuntu bash -c "cd /data/db && tar xvf /backup/backup.tar --> this completed without errors... 5. link a other mongo container to ddec84b89544 and check the collections -> there where no collections :/ – immae Jan 12 '16 at 22:53
1

I wanted to make an image to test things around authentication and I has the same issue. I solved it this way:

Dockerfile

FROM mongo

COPY setup.sh /
RUN chmod +x /setup.sh
RUN /setup.sh

ENTRYPOINT ["/usr/bin/mongod"]
CMD ["--auth", "--dbpath=/containerdb"]

setup.sh

#!/bin/bash

mkdir containerdb

mongod --auth --dbpath=/containerdb &

until mongo admin --eval 'db.createUser({"user":"root", "pwd":"password", "roles": [ "root" ] })'
do
  echo 'Mongo not yet ready, waiting 1 sec until retry'
  sleep 1
done

mongod --shutdown --dbpath=/containerdb

Bim! When you run this image you have a mongodb with authentication activated and a root user. The key is to workaround the VOLUME in the base Dockerfile by storing the data somewhere else.

Samuel Rossille
  • 18,940
  • 18
  • 62
  • 90