[英]Recreation of MongoDB container doesnt re-run config.js file in docker-entrypoint-initdb.d
i have a problem with MongoDB. 我有MongoDB的问题。 I am provisioning the file
config.js
to docker-entrypoint-initdb.d
in my docker-compose
file: 我在
docker-entrypoint-initdb.d
docker-compose
文件中将文件config.js
到docker-entrypoint-initdb.d
:
mongo:
image: mongo:latest
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
MONGO_INITDB_DATABASE: dev
volumes:
- ./config.js:/docker-entrypoint-initdb.d/config.js
- mongodbdata:/data/db
The config.js
file looks like this: config.js
文件如下所示:
db.auth('root', 'example');
db = db.getSiblingDB('dev');
db.approver.insert({"email":"some@email.com,"approverType":"APPROVER"});
db.approver.insert({"email":"someother@email.com","approverType":"ACCOUNTANCY"});
When I run docker-compose up -d
for the first time, everything is fine, the two entries are inserted into the database. 当我第一次运行
docker-compose up -d
时,一切都很好,两个条目都插入到数据库中。
But then, I want to add a trird entry, and recreate the container: 但是,我想添加一个trird条目,并重新创建容器:
db.auth('root', 'example');
db = db.getSiblingDB('dev');
db.approver.insert({"email":"some@email.com,"approverType":"APPROVER"});
db.approver.insert({"email":"someother@email.com","approverType":"ACCOUNTANCY"});
db.approver.insert({"email":"another@email.com","approverType":"ACCOUNTANCY"});
I run docker-compose up -d --force-recreate --no-deps mongo
nothing happens. 我运行
docker-compose up -d --force-recreate --no-deps mongo
没有任何反应。 The container is recreated, but the 3rd entry is not there. 容器被重新创建,但第三个条目不存在。
Running docker exec -it dev_mongo_1 mongo docker-entrypoint-initdb.d/config.js
returns: 运行
docker exec -it dev_mongo_1 mongo docker-entrypoint-initdb.d/config.js
返回:
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d44b8e0a-a32c-4da0-a02b-c3f71d6073dd") }
MongoDB server version: 4.0.10
Error: Authentication failed.
Is there a way to recreate the container so the script is re-runned? 有没有办法重新创建容器,以便重新运行脚本? Or to run a mongo command that will re-run the script in a running container?
或者运行mongo命令,该命令将在正在运行的容器中重新运行脚本?
In mongodb's startup script there is check if initialization should be done: https://github.com/docker-library/mongo/blob/40056ae591c1caca88ffbec2a426e4da07e02d57/3.6/docker-entrypoint.sh#L225 在mongodb的启动脚本中,检查是否应该进行初始化: https : //github.com/docker-library/mongo/blob/40056ae591c1caca88ffbec2a426e4da07e02d57/3.6/docker-entrypoint.sh#L225
# check for a few known paths (to determine whether we've already initialized and should thus skip our initdb scripts)
if [ -n "$shouldPerformInitdb" ]; then
...
so probably it's done only once during DB initialization and then, as you keep DB state by using mongodbdata:/data/db
, it won't initialize. 所以可能在数据库初始化期间只执行了一次,然后,当你使用
mongodbdata:/data/db
保持数据库状态时,它不会初始化。
To fix that you can try to type docker-compose down -v
what will delete data from your DB and let you run initialization once again. 要解决此问题,您可以尝试键入
docker-compose down -v
什么将从数据库中删除数据,并让您再次运行初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.