我想用mongodb实例创建一个带有特定数据库和特定数据的docker映像。是否有可能还是我需要先使用mongo image创建容器,然后才创建数据库并将数据加载到正在运行的容器上?
到目前为止,我已经尝试过:我创建了一个dockerfile,如下所示:
1. FROM mongo
2. COPY ./app/dbdata
3. EXPOSE 27017
4. CMD ["sh","-c","mongo && use kanjidb && exit"]
5. CMD ["mongorestore", "--db", "kanjidb", "dbdata", "--host", "mongo:27017"]
第二行复制包含bson数据的文件夹。
感谢您的帮助或提示。
答案 0 :(得分:0)
您应该能够将目录与数据绑定安装到mongo容器将使用的数据目录中,如下所示:
textFont
请注意,这确实意味着您需要确保该目录存在并且可以被容器访问。有关图像https://hub.docker.com/_/mongo的信息,请参见文档中的“注意事项>存储数据的位置”。
答案 1 :(得分:0)
您无法使用预加载的数据创建映像,但是您可以将数据复制到docker映像,并且每当启动容器时,它将首先转储数据,然后启动容器。在构建期间,您需要将.js
或.sh
复制到/docker-entrypoint-initdb.d.
的所有操作,切记不要放纵。
FROM mongo
MONGO_INITDB_DATABASE=test
MONGO_INITDB_ROOT_USERNAME=mongoadmin
MONGO_INITDB_ROOT_PASSWORD=secret
COPY data/initdb.js:/docker-entrypoint-initdb.d/initdb.js
您可以很好地运行容器,进入点将占用dump.js
。
初始化一个新实例
首次启动容器时,它将执行文件 带有扩展名
.sh
和.js
/docker-entrypoint-initdb.d。文件将按字母顺序执行 订购。.js
个文件将由mongo使用数据库执行 由MONGO_INITDB_DATABASE
变量指定(如果存在),或者 否则测试。您也可以在.js脚本中切换数据库。
例如initdb.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 }
})
MongoDb - Export database to js script (similar to rockmongo export)