标签:monogdb服务镜像
① 查看内容,包括写好的Dockerfile和若干脚本等。从GitHub Dockerpool社区账户下载Mongodb镜像项目:
git clone https://github.com/DockerPool/Mongodb.git 并修改文件
[root@docker1 Mongodb]# ls
Dockerfile mongodb-3.2.repo mongodb-linux-x86_64-rhel70-3.6.0.tgz run.sh set_mongodb_password.sh
其中Dockerfile内容为:
[root@docker1 Mongodb]# vi Dockerfile
#设置从我们之前创建的sshd镜像继承
FROM sshd:centos
MAINTAINER waitfish from dockerpool.com
ADD mongodb-3.2.repo /etc/yum.repos.d/
RUN \
yum update -y && \
yum install -y mongodb-org && \ #这里不推荐使用yum安装monodb,实在下载速度太慢
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服务镜像
原文地址:http://blog.51cto.com/sf1314/2048024