时间:2021-07-01 10:21:17 帮助过:55人阅读
yum clean all
# 创建mongodb存放数据文件的文件夹
RUN mkdir -p /data/db
VOLUME /data/db
ENV AUTH yes
# Add run scripts 添加脚本
ADD run.sh /run.sh
ADD set_mongodb_password.sh /set_mongodb_password.sh
RUN chmod 755 ./*.sh
EXPOSE 27017
EXPOSE 28017
CMD ["/run.sh"]
————————————————————————————————————————————————
[root@docker1 Mongodb]# cat run.sh
#!/bin/bash
if [ ! -f /.mongodb_password_set ]; then
/set_mongodb_password.sh
fi
if [ "$AUTH" == "yes" ]; then
export mongodb='/usr/bin/mongod --nojournal --auth --httpinterface --rest'
else
export mongodb='/usr/bin/mongod --nojournal --httpinterface --rest'
fi
if [ ! -f /data/db/mongod.lock ]; then
eval $mongodb
else
export mongodb=$mongodb' --dbpath /data/db'
rm /data/db/mongod.lock
mongod --dbpath /data/db --repair && eval $mongodb
fi
——————————————————————————————————————————————————
[root@docker1 Mongodb]# cat set_mongodb_password.sh
#!/bin/bash
if [ -f /.mongodb_password_set ]; then
echo "MongoDB password already set!"
exit 0
fi
/usr/bin/mongod --smallfiles --nojournal &
PASS=${MONGODB_PASS:-$(pwgen -s 12 1)}
_word=$( [ ${MONGODB_PASS} ] && echo "preset" || echo "random" )
RET=1
while [[ RET -ne 0 ]]; do
echo "=> Waiting for confirmation of MongoDB service startup"
sleep 5
mongo admin --eval "help" >/dev/null 2>&1
RET=$?
done
echo "=> Creating an admin user with a ${_word} password in MongoDB"
mongo admin --eval "db.addUser({user: 'admin', pwd: '$PASS', roles: [ 'userAdminAnyDatabase', 'dbAdminAnyDatabase' ]});"
mongo admin --eval "db.shutdownServer();"
echo "=> Done!"
touch /.mongodb_password_set
echo "========================================================================"
echo "You can now connect to this MongoDB server using:"
echo ""
echo " mongo admin -u admin -p $PASS --host <host> --port <port>"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "========================================================================"
①②③④⑤⑥⑦⑧⑨⑩
【docker】基于Dockerfile构建monogdb服务镜像
标签:monogdb服务镜像