概述
本文主要介绍如何通过mongodb的zip包打docker镜像
docker镜像需要的文件
DOCKERFILE
#基础镜像使用ubuntu16.04
FROM ubuntu:16.04
#作者
MAINTAINER BolingCavalry
#定义工作目录
ENV WORK_PATH /usr/local/
#定义mongodb文件夹名称
#创建数据库文件目录
RUN mkdir -p /data/db /opt /data/logs /data/mongodata /data/configdb /logs
ADD mongodb-linux-x86_64-rhel70-3.6.1.1.zip /opt
COPY library.zip /usr/lib/
#更新,把libssl.so.1.0.0装上,否则无法运行mongodb
RUN apt-get update \
&& apt-get install -y zip \
&& apt-get install -y vim \
#根据官方说明,此处必须升级libcurl3,根据不同的ubantu镜像,升级脚本不同,文中有官方路径说明
&& apt-get install -y libcurl3 openssl
# 解压并将mongodb文件放到自定位置
RUN set -x \
&& unzip /opt/mongodb-linux-x86_64-rhel70-3.6.1.1.zip -d /opt \
&& unzip /usr/lib/library.zip -d /usr/lib/ \
&& cp -rf /usr/lib/library/. /usr/lib/ \
&& rm -rf /usr/lib/library/ \
&& rm /usr/lib/library.zip \
&& mv /opt/mongodb-linux-x86_64-rhel70-3.6.1.1 /usr/local/mongodb \
&& rm -rf /opt/mongodb-linux-x86_64-rhel70-3.6.1.1.zip
#把mongodb的bin目录加入到PATH环境变量中
ENV PATH=$WORK_PATH/mongodb/bin:$PATH
COPY mongod.conf /usr/local/mongodb/
COPY docker-entrypoint.sh /usr/local/bin/
COPY mongodb.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh \
&& ln -s usr/local/bin/mongodb.sh /mongodb.sh \
&& chmod a+x /usr/local/bin/*.sh \
&& chmod a+x /usr/local/mongodb/bin/* \
&& chmod a+x entrypoint.sh \
&& chmod a+x mongodb.sh
ENTRYPOINT ["docker-entrypoint.sh"]
#连接端口
EXPOSE 27017
#启动服务,--rest参数表示开启web服务
CMD ["mongod", "--rest"]
entrypoint.sh
#!/bin/bash
stop_old_mongod()
{
mongodid=`ps -ef | grep mongod | grep -v grep | awk '{print $2}'`
echo "pid is $mongodid"
if [ -z "$mongodid" ]; then
echo "old mongod is not running, do not need to kill mongod"
else
echo "old mongod is running at pid: $mongodid"
echo "ready to kill $mongodid"
kill $mongodid
echo "kill mongod done"
fi
}
check_remove_mongodlock()
{
if [ -f "/data/mongodata/mongod.lock" ]; then
echo "mongod.lock is exist, remove it now"
rm /data/mongodata/mongod.lock
else
echo "mongod.lock is not exist, you are ready to start mongo, now"
fi
}
echo "checking mongod is running or not"
stop_old_mongod
echo "checking mongod.lock is exist or not."
check_remove_mongodlock
./mongodb.sh start&
echo "mongodb is started"
sleep 30000000
mongodb.sh
#!/bin/bash
start()
{
/usr/local/mongodb/bin/mongod -repair --config /usr/local/mongodb/mongod.conf&
}
stop()
{
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongod.conf --shutdown&
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
;;
esac
exit 0
mongod.conf
dbpath=/data/mongodata
logpath=/data/logs/mongod.log
port=27017
fork=true
journal=false
storageEngine=mmapv1
#此处不设置可能无法连接mongodb
bind_ip_all=true
docker镜像打包注意事项
docker 相关文件放在同一个目录
Dockerfile、mongod.conf、mongodb.sh、library.zip,mongo的zip文件、entrypoint.sh
library.zip需要是因为ubantu系统的16.4镜像缺少ssllib.so.10包,需要手动添加,其下载地址如下
链接: https://pan.baidu.com/s/1qxxOmFpC56W414SYJBZCcg 提取码: 6tg8
docker 镜像如何打镜像请参看其他相关文档
docker build -t docke-image-name:tag . # 最后的’ .’ 是必须要的
rancher相关文件
docker-compose.yml
version: '2'
services:
mongo-cluster:
restart: always
environment:
MONGO_SERVICE_NAME: mongo-cluster
CATTLE_SCRIPT_DEBUG: ${debug}
# entrypoint: /opt/rancher/bin/entrypoint.sh
command:
- --replSet
- "${replset_name}"
image: 仓库名/alpine-mongodb:3.6
labels:
io.rancher.container.pull_image: always
io.rancher.scheduler.affinity:container_label_ne: io.rancher.stack_service.name=$${stack_name}/$${service_name}
io.rancher.scheduler.affinity:host_label: ${host_label}
io.rancher.container.hostname_override: container_name
io.rancher.sidekicks: mongo-base, mongo-datavolume
volumes_from:
- mongo-datavolume
- mongo-base
ports:
- 27017:27017/tcp
mongo-base:
restart: always
labels:
io.rancher.scheduler.affinity:container_label_ne: io.rancher.stack_service.name=$${stack_name}/$${service_name}
io.rancher.scheduler.affinity:host_label: ${host_label}
io.rancher.container.hostname_override: container_name
io.rancher.container.start_once: true
image: rancher/mongodb-conf:v0.1.1
stdin_open: true
entrypoint: /bin/true
mongo-datavolume:
labels:
io.rancher.scheduler.affinity:container_label_ne: io.rancher.stack_service.name=$${stack_name}/$${service_name}
io.rancher.scheduler.affinity:host_label: ${host_label}
io.rancher.container.hostname_override: container_name
io.rancher.container.start_once: true
volumes:
- mongodata:/data/db
entrypoint: /bin/true
image: busybox
volumes:
mongodata:
driver: ${VOLUME_DRIVER}
rancher-compose.yml
version: '2'
catalog:
name: "MongoDB"
version: "3.4-rancher1"
description: "MongoDB Replica Set"
uuid: mongodb-1
minimum_rancher_version: v0.46.0
questions:
- variable: replset_name
description: "Name of the MongoDB replicaSet"
label: "ReplicaSet Name"
type: "string"
required: true
default: "rs0"
- variable: host_label
label: "Host Label to MongoDB Tags"
description: |
Host label to use as MongoDB 'value' tag.
Example: 'database'
required: false
type: "string"
- variable: debug
description: "Enable Debug log for Mongo containers"
label: "Debug"
type: "string"
required: false
default: ""
- variable: "VOLUME_DRIVER"
description: "The VOLUME driver to associate with this server"
label: "VOLUME Driver"
required: true
default: "local"
type: enum
options:
- local
- rancher-nfs
- rancher-efs
- rancher-ebs
- variable: mongo_scale
description: "How many containers the MongoDB will scale to?"
label: "mongo scale"
type: "int"
required: true
default: "3"
services:
mongo-cluster:
scale: ${mongo_scale}
retain_ip: true
metadata:
mongo:
yml:
replset.name: "${replset_name}"
启动方式
启动将镜像文件放到镜像仓库中通过rancher启动即可
总结
整个过程中,最大的问题是解决ssllib.so.10包缺失问题,参看官方文档后发现,必须对ubantu16.4镜像进行ssl软件升级
官方地址-https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu
也可以参考我的gitee地址
my-gitee
一盏灯, 一片昏黄; 一简书, 一杯淡茶。 守着那一份淡定, 品读属于自己的寂寞。 保持淡定, 才能欣赏到最美丽的风景! 保持淡定, 人生从此不再寂寞。