[英]Bootstrapping a mongoDB via docker-compose
Let's say I have the following docker compose that consists of 2 services: a nodeJS app and a mongoDB database that is backing it:假设我有以下由 2 个服务组成的 docker compose:一个 nodeJS 应用程序和一个支持它的 mongoDB 数据库:
version: "3.8"
services:
mongodb-local:
image: mongo:latest
restart: unless-stopped
env_file: ./.env
# see: https://stackoverflow.com/questions/52373098/disable-default-authentication-in-mongo-docker
# environment:
# - MONGO_INITDB_ROOT_USERNAME=$MONGODB_USER
# - MONGO_INITDB_ROOT_PASSWORD=$MONGODB_PASSWORD
# - MONGO_INITDB_DATABASE=$MONGODB_DATABASE
ports:
- "27017:27017"
volumes:
- db:/data/db
app:
depends_on:
- mongodb-local
build: .
restart: unless-stopped
env_file: ./.env
ports:
- 3000:3000
environment:
- DB_HOST=mongodb
- DB_USER=$MONGODB_USER
- DB_PASSWORD=$MONGODB_PASSWORD
- DB_NAME=$MONGODB_DATABASE
- DB_PORT=$MONGODB_DOCKER_PORT
stdin_open: true
tty: true
Let's say that the nodeJS code uses mongoose to kind of bootsrap all the schemas and models, in such a way that once the nodeJS app starts, I can hit specific endpoints to register users, add some data, etc.假设 nodeJS 代码使用猫鼬来引导所有模式和模型,这样一旦 nodeJS 应用程序启动,我就可以点击特定端点来注册用户、添加一些数据等。
What I want to do now is to be able to "seed" the DB in such a way that I create users and/or add data "upon start up" so that when doing: docker-compose up
I will get a "seeded" DB with the app ready to use for local develpment.我现在想做的是能够以这样一种方式“播种”数据库,即我“在启动时”创建用户和/或添加数据,这样在执行以下操作时: docker-compose up
我将得到一个“播种”具有可用于本地开发的应用程序的数据库。
Now it's a 2 step process:现在这是一个两步过程:
What I want is a mechanism to "seed" the DB with a few test data upon doing a docker-compose up immediately.我想要的是一种在立即执行 docker-compose up 时用一些测试数据“播种”数据库的机制。
I tried using the curl image like so:我试过像这样使用卷曲图像:
version: "3.8"
services:
mongodb-local:
image: mongo:latest
restart: unless-stopped
env_file: ./.env
# see: https://stackoverflow.com/questions/52373098/disable-default-authentication-in-mongo-docker
# environment:
# - MONGO_INITDB_ROOT_USERNAME=$MONGODB_USER
# - MONGO_INITDB_ROOT_PASSWORD=$MONGODB_PASSWORD
# - MONGO_INITDB_DATABASE=$MONGODB_DATABASE
ports:
- "27017:27017"
volumes:
- db:/data/db
app:
depends_on:
- mongodb-local
build: .
restart: unless-stopped
env_file: ./.env
ports:
- 3000:3000
environment:
- DB_HOST=mongodb
- DB_USER=$MONGODB_USER
- DB_PASSWORD=$MONGODB_PASSWORD
- DB_NAME=$MONGODB_DATABASE
- DB_PORT=$MONGODB_DOCKER_PORT
stdin_open: true
tty: true
curl-run:
depends_on:
- mongodb-local
- app
image: curlimages/curl
command: "sleep 20 && curl --location --request POST 'http://localhost:3000/register' \
--header 'Content-Type: application/json' \
--data-raw '{\
'username': 'user',\
'password': 'pass',\
'role': 'admin'\
}'"
volumes:
db:
but, no luck... Any pointers would be greatly appreciated.但是,没有运气......任何指针将不胜感激。
I have read about mongodump
and restore
but didn't manage to get that working.我已经阅读了有关mongodump
和restore
的信息,但没有设法使其正常工作。
Note that if there is an approach to do it directly via a volume or directly against the DB container that would be the best!请注意,如果有一种方法可以直接通过卷或直接针对 DB 容器执行此操作,那将是最好的!
Thanks in advance!提前致谢!
Have you tried using /docker-entrypoint-initdb.d
?您是否尝试过使用/docker-entrypoint-initdb.d
?
From the documentation :从文档中:
When a container is started for the first time it will execute files with extensions.sh and.js that are found in /docker-entrypoint-initdb.d.当容器第一次启动时,它会执行扩展名为 .sh 和 .js 的文件,这些文件位于 /docker-entrypoint-initdb.d 中。 Files will be executed in alphabetical order.文件将按字母顺序执行。 .js files will be executed by mongosh (mongo on versions below 6) using the database specified by the MONGO_INITDB_DATABASE variable, if it is present, or test otherwise. .js 文件将由 mongosh(6 以下版本的 mongo)使用 MONGO_INITDB_DATABASE 变量指定的数据库(如果存在)执行,否则进行测试。 You may also switch databases within the.js script.您还可以在 .js 脚本中切换数据库。
The only caveat I see here is that this likely only works on a fresh build ( docker-compose up -b
) vs. every time it is started.我在这里看到的唯一警告是,这可能只适用于全新构建 ( docker-compose up -b
) 而不是每次启动时。 If you don't care about preserving the data between ups/downs, this should work.如果您不关心在起伏之间保留数据,这应该可行。
I managed to fix this by leveraging a tweak on the mongo image entrypoint: /docker-entrypoint-initdb.d
我设法通过对 mongo 图像入口点进行调整来解决此问题: /docker-entrypoint-initdb.d
Essentially, I copy an init file, in Javascript, that gets copied over into the container entrypoint via the volume configuration and that gets executed on startup, which allows me to achieve what I want: seed the DB with data upon startup!本质上,我在 Javascript 中复制了一个 init 文件,该文件通过卷配置复制到容器入口点,并在启动时执行,这使我能够实现我想要的:在启动时为数据库播种数据!
Here's my modified docker service for mongoDB:这是我为 mongoDB 修改的 docker 服务:
services:
mongodb-local:
image: mongo:latest
restart: unless-stopped
env_file: ./.env
ports:
- "27017:27017"
volumes:
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
- db:/data/db
And, here's my actual "seed" file:而且,这是我实际的“种子”文件:
print('Start #################################################################');
db = db.getSiblingDB('<some db name>');
console.log(db)
db.createCollection('<a collection>');
db.dams.insertMany([ ....]);
....
print("End #########")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.