在Dockerfile中的Mongorestore [英] Mongorestore in a Dockerfile

查看:182
本文介绍了在Dockerfile中的Mongorestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个启动mongo服务器的Docker映像,并在启动时自动从以前的mongodump恢复.

I want to create a Docker image that starts a mongo server and automatically restores from a previous mongodump on startup.

这是该映像的Dockerfile:

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


有什么方法可以使mongorestore命令通过Docker运行?


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

推荐答案

此答案(马克·杨的答案)的帮助下,以及Dockerfile参考,我能够使它正常工作.

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

Dockerfile

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

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

这篇关于在Dockerfile中的Mongorestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆