将json格式的数据导入mongo容器docker
我有个问题。我开始学docker了。我知道如何创建一个容器,现在我尝试将数据(json)导入到我的容器mongo中,但我不知道如何做到这一点。你能帮我给出一个简单的解决方案吗?和Docker一起创作 我的集装箱-mongo 我的文件-data.json将json格式的数据导入mongo容器docker,docker,docker-compose,Docker,Docker Compose,我有个问题。我开始学docker了。我知道如何创建一个容器,现在我尝试将数据(json)导入到我的容器mongo中,但我不知道如何做到这一点。你能帮我给出一个简单的解决方案吗?和Docker一起创作 我的集装箱-mongo 我的文件-data.json 谢谢 不能简单地使用docker compose导入JSON。您需要将数据放在js文件中,并使用/docker entrypoint initdb.d 当容器第一次启动时,它将执行文件 扩展名为.sh和.js,可在 /docker入口点initd
谢谢 不能简单地使用docker compose导入JSON。您需要将数据放在js文件中,并使用
/docker entrypoint initdb.d
当容器第一次启动时,它将执行文件
扩展名为.sh
和.js
,可在
/docker入口点initdb.d
。文件将按字母顺序执行
秩序.js
文件将由mongo使用数据库执行
由MONGO\u INITDB\u数据库
变量指定(如果存在),或
否则测试。您还可以在.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 }
});
docker compose
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:27017
我不建议docker commit,最好将js文件复制到docker build,复制init.js/docker entrypoint initdb.d/,