如何使用docker-compose种植一个mongo数据库? [英] How do I seed a mongo database using docker-compose?

查看:439
本文介绍了如何使用docker-compose种植一个mongo数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试分发一组连接的应用程序,这些应用程序运行在几个链接的容器中,其中包含一个需要的mongo数据库:

I am trying to distribute a set of connected applications running in several linked containers that includes a mongo database that is required to:


  • 包含一些种子数据;

  • 允许用户添加其他数据。

理想情况下数据也将被保留在链接的数据卷容器中。

Ideally the data will also be persisted in a linked data volume container.

我可以将数据存入 mongo 容器中一个不装载任何卷的 mongo 基本实例(dockerhub image: psychemedia / mongo_nomount - 这本质上是基础没有 VOLUME / data / db 语句的mongo Docker文件)和一条 Dockerfile 配置,如下所示:

I can get the data into the mongo container using a mongo base instance that doesn't mount any volumes (dockerhub image: psychemedia/mongo_nomount - this is essentially the base mongo Dockerfile without the VOLUME /data/db statement) and a Dockerfile config along the lines of:

ADD . /files
WORKDIR /files
RUN mkdir -p /data/db && mongod --fork --logpath=/tmp/mongodb.log && sleep 20 && \
mongoimport  --db testdb --collection testcoll  --type csv --headerline --file ./testdata.csv  #&& mongod --shutdown

其中 ./ testdata.csv 与Dockerfile位于同一目录( ./ mongo-with-data )。

where ./testdata.csv is in the same directory (./mongo-with-data) as the Dockerfile.

我的码头-compose配置文件包括以下内容:

My docker-compose config file includes the following:

mongo:
  #image: mongo
  build: ./mongo-with-data
  ports:
    - "27017:27017"
  #Ideally we should be able to mount this against a host directory
  #volumes:
  #  - ./db/mongo/:/data/db
  #volumes_from:
  #  - devmongodata

#devmongodata:
#    command: echo created
#    image: busybox
#    volumes: 
#       - /data/db

每当我尝试挂载VOLUME时,似乎删除了存储在 / data / db 中的原始种子数据。我猜,当一个卷被安​​装到 / data / db 时,它会替换当前的任何内容。

Whenever I try to mount a VOLUME it seems as if the original seeded data - which is stored in /data/db - is deleted. I guess that when a volume is mounted to /data/db it replaces whatever is there currently.

那就是说,停泊港用户指南建议:容器在创建时被初始化。如果容器的基本映像包含指定装入点的数据,那么在卷初始化时将现有数据复制到新卷中?因此,如果在播种 RUN 命令之后放置VOLUME命令,我预计数据将持续存在。

That said, the docker userguide suggests that: Volumes are initialized when a container is created. If the container’s base image contains data at the specified mount point, that existing data is copied into the new volume upon volume initialization? So I expected the data to persist if I placed the VOLUME command after the seeding RUN command?

那么什么我做错了?

长视图是我想自动化几个链接的容器的构建,然后分发一个 Vagrantfile / docker-组合YAML文件,其将启动一组链接的应用程序,其中包括具有(部分预填充)持久数据的预种子 mongo 数据库容器。

The long view is that I want to automate the build of several linked containers, and then distribute a Vagrantfile/docker-compose YAML file that will fire up a set of linked apps, that includes a pre-seeded mongo database with a (partially pre-populated) persistent data container.

推荐答案

我使用另一个docker容器来完成此操作,其唯一目的是种植mongo,然后退出。我怀疑这是与 ebaxt 相同的想法,但是当我正在寻找答案时,我只是想看到一个快速而肮脏但直截了当的例子。所以这里是我的:

I do this using another docker container whose only purpose is to seed mongo, then exit. I suspect this is the same idea as ebaxt's, but when I was looking for an answer to this, I just wanted to see a quick-and-dirty, yet straightforward, example. So here is mine:

docker-compose.yml

mongodb:
  image: mongo
  ports:
    - "27017:27017"

mongo-seed:
  build: ./mongo-seed
  links:
    - mongodb

# my webserver which uses mongo (not shown in example)
webserver:
  build: ./webserver
  ports:
    - "80:80"
  links:
    - mongodb

mongo-seed / Dockerfile

FROM mongo

COPY init.json /init.json
CMD mongoimport --host mongodb --db reach-engine --collection MyDummyCollection --type json --file /init.json --jsonArray

mongo-seed / init.json

[
  {
    "name": "Joe Smith",
    "email": "jsmith@gmail.com",
    "age": 40,
    "admin": false
  },
  {
    "name": "Jen Ford",
    "email": "jford@gmail.com",
    "age": 45,
    "admin": true
  }
]

这篇关于如何使用docker-compose种植一个mongo数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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