我想创建一个启动 mongo 服务器并自动从以前的版本恢复的 Docker 映像mongodump
启动时。
这是我的图像 Dockerfile:
FROM mongo
COPY dump /home/dump
CMD mongorestore /home/dump
当我运行这个时,我遇到了这个错误:
Failed: error connecting to db server: no reachable servers
有什么办法可以得到mongorestore
通过 Docker 运行的命令?
在来自的帮助下这个答案 https://stackoverflow.com/a/20354167/3692084,Marc Young 的回答,以及 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
#!/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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)