初始化码头化的mongo数据 [英] Initialize data on dockerized mongo

查看:127
本文介绍了初始化码头化的mongo数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想用一些初始化数据创建一个mongo图像。



任何想法?

解决方案

首先创建Docker卷

  docker volume create --name mongostore 

然后创建您的mongo容器

  docker run -d --name mongo -v mongostore:/ data / db mongo:latest 

这里的$ code> -v 开关负责安装卷 mongostore / data / db 位置,这是mongo保存其数据的位置。卷是持久的(在主机上)。即使没有容器运行,您将看到您的mongostore卷列出了

  docker volume ls 

您可以杀死容器并创建一个新的(与上述相同的行),新的mongo容器将接收上一个容器的状态。



初始化卷
如果没有,Mongo将初始化新数据库。这是负责在mongostore中创建初始数据。假设您要使用预种子数据库创建一个全新的环境。问题成为如何在创建mongo容器之前将数据从本地环境(例如)传输到卷。我会列出两种情况。


  1. 本地环境



    你使用Docker for Mac / Windows或Docker Toolbox。在这种情况下,您可以轻松地将本地驱动器安装到临时容器中以初始化卷。例如:

      docker run --rm -v / Users / myname / work / mongodb:/ incoming \ 
    -v mongostore:/ data alpine:3.4 cp -rp / incoming / * / data

    t为云存储工作。在这种情况下,您需要复制文件。


  2. 远程环境(AWS,GCP,Azure,...)



    这是一个很好的主意,tar /压缩的东西加快上传。

      tar czf mongodata.tar .gz / Users / myname / work / mongodb 

    然后创建一个临时容器来解压缩并复制文件到蒙太子 tail -f / dev / null 只是确保容器不退出。

      docker run -d --name temp -v mongostore:/ data alpine:3.4 tail -f / dev / null 

    将文件复制到

      docker cp mongodata.tar.gz temp: 

    解开并移动到卷

      docker exec temp tar xzf mongodata.tar.gz&& cp -rp mongodb / * / data 

    清理

      docker rm temp 


您还可以将文件复制到远程主机并从那里安装,但我倾向于避免与远程主机进行交互。



免责声明。我从记忆中写下来(没有测试)。


I'm running a dockerized mongo container.

I'd like to create a mongo image with some initialized data.

Any ideas?

解决方案

First create a docker volume

docker volume create --name mongostore

then create your mongo container

docker run -d --name mongo -v mongostore:/data/db mongo:latest

The -v switch here is responsible for mounting the volume mongostore at the /data/db location, which is where mongo saves its data. The volume is persistent (on the host). Even with no containers running you will see your mongostore volume listed by

docker volume ls

You can kill the container and create a new one (same line as above) and the new mongo container will pick up the state of the previous container.

Initializing the volume Mongo initializes a new database if none is present. This is responsible for creating the initial data in the mongostore. Let's say that you want to create a brand new environment using a pre-seeded database. The problem becomes how to transfer data from your local environment (for instance) to the volume before creating the mongo container. I'll list two cases.

  1. Local environment

    You're using either Docker for Mac/Windows or Docker Toolbox. In this case you can easily mount a local drive to a temporary container to initialize the volume. Eg:

    docker run --rm -v /Users/myname/work/mongodb:/incoming \
      -v mongostore:/data alpine:3.4 cp -rp /incoming/* /data
    

    This doesn't work for cloud storage. In that case you need to copy the files.

  2. Remote environment (AWS, GCP, Azure, ...)

    It's a good idea to tar/compress things up to speed the upload.

    tar czf mongodata.tar.gz /Users/myname/work/mongodb
    

    Then create a temporary container to untar and copy the files to the mongostore. the tail -f /dev/null just makes sure that the container doesn't exit.

    docker run -d --name temp -v mongostore:/data alpine:3.4 tail -f /dev/null
    

    Copy files to it

    docker cp mongodata.tar.gz temp:.
    

    Untar and move to the volume

    docker exec temp tar xzf mongodata.tar.gz && cp -rp mongodb/* /data
    

    Cleanup

    docker rm temp
    

You could also copy the files to the remote host and mounting from there but I tend to avoid interacting with the remote host at all.

Disclaimer. I'm writing this from memory (no testing).

这篇关于初始化码头化的mongo数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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