我有个问题。我开始学码头了。我知道如何创建一个容器,现在我尝试将数据(Json)导入到我的容器mongo,但我不知道如何实现。你能帮我并给出一个简单的解决方案吗?与码头-写作。
我的容器- mongo我的文件- data.json
谢谢!
发布于 2019-10-02 13:36:13
您不能简单地使用导入JSON。您需要将数据放在js文件中并使用/docker-entrypoint-initdb.d挂载该/docker-entrypoint-initdb.d文件。
当容器第一次启动时,它将执行在
.sh中找到的扩展名为/docker-entrypoint-initdb.d和.js的文件。文件将按字母顺序执行。如果存在.js文件,将使用MONGO_INITDB_DATABASE变量指定的数据库由mongo执行,或者以其他方式进行测试。您还可以在.js脚本中切换数据库。
示例js文件:
db = db.getSiblingDB("test");
db.article.drop();
db.article.save( {
title : "this is my title" ,
author : "bob" ,
posted : new Date(1079895594000) ,
pageViews : 5 ,
tags : [ "fun" , "good" , "fun" ] ,
comments : [
{ author :"joe" , text : "this is cool" } ,
{ author :"sam" , text : "this is bad" }
],
other : { foo : 5 }
});
db.article.save( {
title : "this is your title" ,
author : "dave" ,
posted : new Date(4121381470000) ,
pageViews : 7 ,
tags : [ "fun" , "nasty" ] ,
comments : [
{ author :"barbara" , text : "this is interesting" } ,
{ author :"jenny" , text : "i like to play pinball", votes: 10 }
],
other : { bar : 14 }
});
db.article.save( {
title : "this is some other title" ,
author : "jane" ,
posted : new Date(978239834000) ,
pageViews : 6 ,
tags : [ "nasty" , "filthy" ] ,
comments : [
{ author :"will" , text : "i don't like the color" } ,
{ author :"jenny" , text : "can i get that in green?" }
],
other : { bar : 14 }
});码头工
mongo:
image: mongo
container_name: mongo1
environment:
MONGO_INITDB_ROOT_USERNAME: test
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: test
volumes:
- ./init.js:/docker-entrypoint-initdb.d/init.js
ports:
- 27017:27017https://stackoverflow.com/questions/58202154
复制相似问题