How to save MongoDB - data about the running Docker container in a new image?

I have a working Mongo DB Container called xyz from official Mongo Image. I created a 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 to a new image on dockerhub.

I used docker commitand created a new image, clicked it on the docker hub. If I pull out the image on another system and create a new container from this image, there is no data in my original xyz container.

After researching, I found out that a mango image can be used with a volume, but I skipped this step in the past ... I think that using the / data / db container as a standard volume ( docker inspect), to fix this volume is not tied to a new image? !

I also tried docker export/importwith the same problem mentioned above!

Now my question is, how can I achieve the migration of this "xyz" container with my data samples in a new image / container? Many thanks!

+4
source share
2 answers

, . , xyz .

mongo docker volume. , docker commit. docker export .

"xyz" /?

:

, SO .

+4

, , , . :

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

! , mongodb root. , VOLUME Docker, .

+2

Source: https://habr.com/ru/post/1623999/


All Articles