Mongorestore in a Dockerfile

前端 未结 5 821
借酒劲吻你
借酒劲吻你 2021-02-05 07:10

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

5条回答
  •  感情败类
    2021-02-05 07:50

    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

提交回复
热议问题