25

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 the image:

 FROM mongo

 COPY dump /home/dump

 CMD mongorestore /home/dump

When I run this, I run into this error:

Failed: error connecting to db server: no reachable servers


Is there any way to get the mongorestore command to run through Docker?

5 Answers 5

19

With help from this answer, Marc Young's answer, and the Dockerfile reference I was able to get this working.


Dockerfile

FROM mongo

COPY dump /home/dump
COPY mongo.sh /home/mongo.sh
RUN chmod 777 /home/mongo.sh

CMD /home/mongo.sh

mongo.sh

#!/bin/bash

# Initialize a mongo data folder and logfile
mkdir -p /data/db
touch /var/log/mongodb.log
chmod 777 /var/log/mongodb.log

# Start mongodb with logging
# --logpath    Without this mongod will output all log information to the standard output.
# --logappend  Ensure mongod appends new entries to the end of the logfile. We create it first so that the below tail always finds something
/entrypoint.sh mongod --logpath /var/log/mongodb.log --logappend &

# Wait until mongo logs that it's ready (or timeout after 60s)
COUNTER=0
grep -q 'waiting for connections on port' /var/log/mongodb.log
while [[ $? -ne 0 && $COUNTER -lt 60 ]] ; do
    sleep 2
    let COUNTER+=2
    echo "Waiting for mongo to initialize... ($COUNTER seconds so far)"
    grep -q 'waiting for connections on port' /var/log/mongodb.log
done

# Restore from dump
mongorestore --drop /home/dump

# Keep container running
tail -f /dev/null

4
  • 1
    I moved the mongorestore command to the Dockerfile to make the script more reusable: eval $MONGORESTORE_CMD in mongo.sh and RUN MONGORESTORE_CMD='mongorestore --drop /home/dump' in the Dockerfile. I thought it made sense also to call the script /entrypoint-with-mongorestore.sh finally. Commented Mar 11, 2017 at 20:03
  • 3
    Does the database have to be restored everytime the image is created? Shouldn't this be a "RUN" instead of a CMD? ["CMD /home/mongo.sh"]
    – Ashu Joshi
    Commented May 19, 2017 at 14:53
  • 1
    In trying this approach, I get /entrypoint.sh: No such file or directory. Changing ./entrypoint.sh to docker-entrypoint.sh (note the change from absolute to relative) did the trick.
    – Laizer
    Commented Dec 19, 2017 at 18:02
  • I also think the same as @AshuJoshi, wouldn't this restore the database every time the image is run? Commented Jul 22, 2021 at 13:47
14

A similar solution to RyanNHG's, but without an sh file.

Dockerfile

FROM mongo:3.6.8

COPY dump/ /tmp/dump/

CMD mongod --fork --logpath /var/log/mongodb.log; \
    mongorestore /tmp/dump/; \
    mongod --shutdown; \
    docker-entrypoint.sh mongod
3
  • 3
    This works like a charm Thanks @hisener. I have been struggling for whole day then found this with little tweaks to this it helped me a lot.
    – Gaurav
    Commented Mar 7, 2020 at 9:09
  • Works, but the /data/db files are all deleted after running mongod --shutdown
    – Justin
    Commented Mar 1, 2022 at 22:42
  • Was able to get it working within a multi-stage build by copying /data/db to /tmp/db before mongod --shutdown, then within the second stage, copying from the build-stage's /tmp/db to /data/db.
    – Justin
    Commented Mar 2, 2022 at 0:01
11

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

3

the problem isn't with docker.

If you look at the dockerfile for mongo it runs CMD ["mongod"] which starts the mongo service.

You said FROM MONGO but you overwrote the CMD line. this means mongo never started via mongod. so try CMD mongod; mongorestore /home/dump

2
  • Thank you for your reply! And you're correct, I took a look at that file earlier tonight and realized that only the last CMD will run. However, with CMD mongod; mongorestore /home/dump, the mongorestore command never runs. I tried mongod & mongorestore /home/dump and got a little bit closer. Still no luck though.
    – RyanNHG
    Commented Sep 2, 2016 at 4:15
  • mongod & mongorestore /home/dump will not work since mongod is running in the foreground and thus is blocking. Use the answer from ryanHNG Commented Sep 2, 2016 at 7:50
0

You probably don't want to use this for production but it does what you need:

== Dockerfile ==
FROM mongo:3

COPY restore.sh /restore.sh
COPY ./mongodump /dump/

ENTRYPOINT /restore.sh

then

== restore.sh ==
#!/usr/bin/env bash

# Execute restore in the background after 5s
# https://docs.docker.com/engine/reference/run/#detached--d
sleep 5 && mongorestore /dump &

# Keep mongod in the foreground, otherwise the container will stop
docker-entrypoint.sh mongod

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.