如何持久化MongoDB-正在新映像中运行的Docker容器的数据? [英] Howto persist MongoDB - data of a running Docker container in a new image?

查看:262
本文介绍了如何持久化MongoDB-正在新映像中运行的Docker容器的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在运行的Mongo数据库容器,它来自官方的Mongo Image,名为xyz.我用docker run -d -p 21707:21707 mongo创建了容器 在这个容器中,我创建了2个带有示例数据的集合.

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.

现在,我想将此容器提取到dockerhub上的新映像中.

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

我使用了docker commit并创建了一个新映像,将其推送到docker hub上.如果我在其他系统上拉取映像并从该映像创建新容器,则没有像我的原始容器xyz那样的数据.

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.

经过研究,我发现mongo映像可以与一个卷一起使用,但是我错过了过去的这一步骤...我认为容器在提交此内容时将/data/db用作标准卷(docker inspect)音量未附加到新图像?!

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?!

我也尝试了docker export/import的上述相同问题!

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

现在我的问题是,我如何才能将带有示例数据的"xyz"运行容器迁移到新的图像/容器中? 非常感谢!

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!

推荐答案

我使用了docker commit并创建了一个新映像,将其推送到docker hub上.如果我将图像拉到另一个系统上并从该图像创建新容器,则没有像我的原始容器xyz这样的数据.

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.

mongo docker映像将其数据写入 volume .结果,这些数据将不会保存到使用docker commit创建的新docker映像中. docker export由于相同的原因不会产生您的数据.

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.

我如何才能将带有样本数据的"xyz"运行容器迁移到新的图像/容器中?

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

您想要的是:

  • 使用第一个容器的卷重新创建一个新容器→请参阅 --volumes-from
  • 将第一个容器的卷数据保存到docker主机上的目录中,然后创建一个新容器 mounting 将此目录放入容器中→docker run
  • 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

此外,这个 SO问题可能会帮助您确定音量.

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

这篇关于如何持久化MongoDB-正在新映像中运行的Docker容器的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆