Reputation: 11
I would like to create a docker image with mongodb instance with a specific database and specific data in it. Is it possible or do I need to use mongo image to create the container first and only then create the db and load the data on a running container?
What I have tried so far: I have created a dockerfile as follows:
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"]
The second line copies folder with bson data.
Thank you for your help or hints.
Upvotes: 1
Views: 2041
Reputation: 60114
You can not create image with pre-loaded data, but you can copy your data to docker image and whenever you start the container it will first dump your data and then start the container. all you need to copy .js
or .sh
to /docker-entrypoint-initdb.d.
during build time, remember not bason.
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
you are good to run the container and entrypoint will take of dump.js
.
Initializing a fresh instance
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. Files will be executed in alphabetical order..js
files will be executed by mongo using the database specified by theMONGO_INITDB_DATABASE
variable, if it is present, or test otherwise. You may also switch databases within the .js script.
for example the 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)
Upvotes: 2
Reputation: 424
You should be able to bind-mount a directory with your data to the data directory the mongo container will use like so:
$ docker ... -v /your/data/dir:/data/db ... mongo
Please note that this does mean you will need to ensure this directory exists and is accessible by the container. See 'Caveats > Where to Store Data' at the documentation for the image https://hub.docker.com/_/mongo .
Upvotes: 1