如何使用 docker-compose 为 mongo 数据库做种? [英] How do I seed a mongo database using docker-compose?
问题描述
我正在尝试分发一组在多个链接容器中运行的连接应用程序,其中包含需要的 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 图像:psychemedia/mongo_nomount
- 这本质上是基本的 mongo Dockerfile 没有 VOLUME/data/db
语句)和一个 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.
我的 docker-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.
也就是说,docker userguide 建议:卷在容器已创建.如果容器的基础镜像包含指定挂载点的数据,那么在卷初始化时将现有数据复制到新卷中?因此,如果我在播种 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-compose 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屋!