Mongodb Dockerfile中的Mongorestore
我想创建一个Docker映像,启动mongo服务器,并在启动时自动从以前的Mongodb Dockerfile中的Mongorestore,mongodb,docker,dockerfile,mongorestore,Mongodb,Docker,Dockerfile,Mongorestore,我想创建一个Docker映像,启动mongo服务器,并在启动时自动从以前的mongodump恢复 以下是我的图像摘要文件: FROM mongo COPY dump /home/dump CMD mongorestore /home/dump 运行此操作时,我遇到以下错误: 失败:连接到数据库服务器时出错:没有可访问的服务器 有没有办法让mongorestore命令通过Docker运行?问题不在于Docker 如果您查看它运行的CMD[“mongod”]启动mongo服务 您在M
mongodump
恢复
以下是我的图像摘要文件:
FROM mongo
COPY dump /home/dump
CMD mongorestore /home/dump
运行此操作时,我遇到以下错误:
失败:连接到数据库服务器时出错:没有可访问的服务器
有没有办法让
mongorestore
命令通过Docker运行?问题不在于Docker
如果您查看它运行的CMD[“mongod”]
启动mongo服务
您在MONGO中说了,但您重写了CMD
行。这意味着mongo从未通过mongod
启动。所以试试cmdmongod;mongorestore/home/dump
在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
#!/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
mongo.sh
FROM mongo
COPY dump /home/dump
COPY mongo.sh /home/mongo.sh
RUN chmod 777 /home/mongo.sh
CMD /home/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 ==
FROM mongo:3
COPY restore.sh /restore.sh
COPY ./mongodump /dump/
ENTRYPOINT /restore.sh
然后
与RyanNHG的解决方案类似,但没有sh文件
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
这是一个老问题,上面的解决方案仍然可以使用,但在以后的版本中,您可以在/docker entrypoint initdb.d/
中添加.sh和.js脚本,在实例首次加载时执行(/data/db
为空)
现在,Dockerfile可能看起来像:
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
这样,在容器第一次启动时,import\u data.sh
中的任何文件(或您在其中的任何其他文件)都将运行
# change the mongorestore command to match your case, adding user/password and other options.
mongorestore /tmp/dump # note we can possibly restore many DBs.
它记录在初始化新实例的部分中感谢您的回复!你是对的,我今晚早些时候查看了该文件,发现只有最后一个CMD
会运行。但是,使用CMD mongod;mongorestore/home/dump
,mongorestore
命令永远不会运行。我尝试了mongod&mongorestore/home/dump
,然后离得更近了一点。但是仍然没有运气。mongod和mongorestore/home/dump将无法工作,因为mongod在前台运行,因此被阻塞。使用Ryanngi的答案将mongorestore
命令移动到Dockerfile
以使脚本更易于重用:mongo.sh中的eval$mongorestore\u CMD
和Dockerfile
中的运行mongorestore\u CMD='mongorestore--drop/home/dump'
。我认为用mongorestore.sh调用脚本/entrypoint也是有意义的。是否每次创建图像时都必须恢复数据库?这不应该是“RUN”而不是CMD吗?[“CMD/home/mongo.sh”]在尝试这种方法时,我得到了/entrypoint.sh:没有这样的文件或目录。将/entrypoint.sh
更改为docker entrypoint.sh
(注意从绝对更改为相对)成功了。这就像一个符咒一样,感谢@hisener。我已经挣扎了一整天,然后发现这个小调整,这对我帮助很大。