Skip to content

Commit

Permalink
mongodb,redis: refine startup script
Browse files Browse the repository at this point in the history
  • Loading branch information
packageman committed Sep 13, 2016
1 parent 5272386 commit 4876231
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 15 deletions.
16 changes: 10 additions & 6 deletions mongodb/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@ FROM packageman/base:1.0.1

MAINTAINER Byron Zhang <[email protected]>

ENV MONGO_VERSION ${MONGO_VERSION:-3.2.7}

RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927 \
&& echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" > /etc/apt/sources.list.d/mongodb-org.list\
&& apt-get update \
&& DEBIAN_FRONTEND="noninteractive" apt-get install -y \
mongodb-org=3.2.7 \
mongodb-org-server=3.2.7 \
mongodb-org-shell=3.2.7 \
mongodb-org-mongos=3.2.7 \
mongodb-org-tools=3.2.7 --no-install-recommends
mongodb-org=${MONGO_VERSION} \
mongodb-org-server=${MONGO_VERSION} \
mongodb-org-shell=${MONGO_VERSION} \
mongodb-org-mongos=${MONGO_VERSION} \
mongodb-org-tools=${MONGO_VERSION} --no-install-recommends

ADD mongod.conf /etc/mongod.conf

RUN mkdir -p /etc/service/mongodb
ADD start.sh /etc/service/mongodb/run
RUN chmod +x /etc/service/mongodb/run

EXPOSE 27017

VOLUME /var/lib/mongodb
VOLUME ["/var/log/mongodb", "/var/lib/mongodb"]

RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
22 changes: 22 additions & 0 deletions mongodb/mongod.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# mongod.conf

# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/

storage:
dbPath: /var/lib/mongodb
journal:
enabled: true

systemLog:
destination: file
logAppend: true
logRotate: reopen
path: /var/log/mongodb/mongod.log

net:
port: 27017
bindIp: 0.0.0.0

security:
authorization: enabled
29 changes: 27 additions & 2 deletions mongodb/start.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
#!/bin/bash
#! /bin/bash

/sbin/setuser mongodb mongod --dbpath /var/lib/mongodb --bind_ip 0.0.0.0
set -e
exec 2>&1

pidFile=/tmp/mongod.pid
configFile=/etc/mongod.conf
userInitializedFile=/tmp/mongoUserInitialized

function init_mongo_user {
/sbin/setuser mongodb mongod -f /etc/mongod.conf --noauth --pidfilepath $pidFile --fork

username=${MONGO_USERNAME:-"admin"}
password=${MONGO_USERNAME:-"Abc123__"}

echo "db.createUser({user: '${username}', pwd: '${password}', roles: ['root']})" | mongo admin
echo "username: $username, password: $password, role: root" > $userInitializedFile

kill -2 `cat $pidFile`
rm $pidFile
}


if [[ $INIT_MONGO_USER ]] && [[ ! -f $userInitializedFile ]]; then
init_mongo_user
fi

/sbin/setuser mongodb mongod -f /etc/mongod.conf
6 changes: 2 additions & 4 deletions redis/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ RUN add-apt-repository ppa:chris-lea/redis-server \
&& DEBIAN_FRONTEND="noninteractive" apt-get install -y \
redis-server --no-install-recommends

RUN sed 's/^daemonize yes/daemonize no/' -i /etc/redis/redis.conf \
&& sed 's/^bind 127.0.0.1/bind 0.0.0.0/' -i /etc/redis/redis.conf \
&& sed '/^logfile/d' -i /etc/redis/redis.conf
ADD redis.conf /etc/redis/redis.conf

RUN mkdir -p /etc/service/redis
ADD start.sh /etc/service/redis/run
RUN chmod +x /etc/service/redis/run

EXPOSE 6379

VOLUME /var/lib/redis
VOLUME ["/var/lib/redis", "/var/log/redis/redis.log"]

RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
11 changes: 11 additions & 0 deletions redis/redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
daemonize no

port 6379

bind 0.0.0.0

logfile /var/log/redis/redis.log

dir /var/lib/redis

dbfilename dump.rdb
11 changes: 8 additions & 3 deletions redis/start.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/bin/bash

if [ $PASS ]; then
echo "requirepass $PASS" >> /etc/redis/redis.conf
set -e
exec 2>&1

configFile=/etc/redis/redis.conf

if [ $REDIS_PASSWORD ]; then
echo "requirepass $REDIS_PASSWORD" >> $configFile
fi

service redis-server start
/sbin/setuser redis redis-server $configFile

0 comments on commit 4876231

Please sign in to comment.