I want to create a Docker image that starts a mongo server and automatically restores from a previous mongodump
on startup.
Here is my Dockerfile for
This is an old question and the solution above could still work but in later versions, you can add .sh and .js scripts in /docker-entrypoint-initdb.d/
, which will be executed in case the instance is first loading (/data/db
is empty).
Now, Dockerfile could look something like:
FROM mongo
COPY ./data-dump/ /tmp/dump/mydb/
COPY ./import_data.sh /docker-entrypoint-initdb.d/import_data.sh
CMD chmod 777 /docker-entrypoint-initdb.d/import_data.sh #this is probably too permissive
With that, whatever is in import_data.sh
will be run (or whatever other file(s) you have there) first time the container is started.
# change the mongorestore command to match your case, adding user/password and other options.
mongorestore /tmp/dump # note we can possibly restore many DBs.
It is documented here under Initializing a fresh instance section