Created
November 24, 2016 02:38
-
-
Save ajeetraina/8d18feca9a1711c5d9fcd42638e5d61b to your computer and use it in GitHub Desktop.
Preparing MongoDB Container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dockerfile: | |
FROM ubuntu:16.04 | |
MAINTAINER Ajeet S Raina <ajeetraina@gmail.com> | |
ENV MONGO_USER=mongodb \ | |
MONGO_DATA_DIR=/var/lib/mongodb \ | |
MONGO_LOG_DIR=/var/log/mongodb | |
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 \ | |
&& echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' > /etc/apt/sources.list.d/mongodb.list \ | |
&& apt-get update \ | |
&& DEBIAN_FRONTEND=noninteractive apt-get install -y mongodb-org-server mongodb-org-shell \ | |
&& sed 's/^bind_ip/#bind_ip/' -i /etc/mongod.conf \ | |
&& rm -rf /var/lib/apt/lists/* | |
COPY entrypoint.sh /sbin/entrypoint.sh | |
RUN chmod 755 /sbin/entrypoint.sh | |
EXPOSE 27017/tcp | |
VOLUME ["${MONGO_DATA_DIR}"] | |
ENTRYPOINT ["/sbin/entrypoint.sh"] | |
CMD ["/usr/bin/mongod"] | |
entrypoint.sh | |
============= | |
#!/bin/bash | |
set -e | |
map_uidgid() { | |
USERMAP_ORIG_UID=$(id -u ${MONGO_USER}) | |
USERMAP_ORIG_GID=$(id -g ${MONGO_USER}) | |
USERMAP_GID=${USERMAP_GID:-${USERMAP_GID:-$USERMAP_ORIG_GID}} | |
USERMAP_UID=${USERMAP_UID:-$USERMAP_ORIG_UID} | |
if [[ ${USERMAP_UID} != ${USERMAP_ORIG_UID} ]] || [[ ${USERMAP_GID} != ${USERMAP_ORIG_GID} ]]; then | |
echo "Mapping UID and GID for ${MONGO_USER}:${MONGO_USER} from ${USERMAP_ORIG_UID}:${USERMAP_ORIG_GID} to ${USERMAP_UID}:${USERMAP_GID}" | |
groupmod -g ${USERMAP_GID} ${MONGO_USER} | |
sed -ie "s/:${USERMAP_ORIG_UID}:${USERMAP_ORIG_GID}:/:${USERMAP_UID}:${USERMAP_GID}:/" /etc/passwd | |
fi | |
} | |
create_data_dir() { | |
mkdir -p ${MONGO_DATA_DIR} | |
chmod -R 0755 ${MONGO_DATA_DIR} | |
chown -R ${MONGO_USER}:${MONGO_USER} ${MONGO_DATA_DIR} | |
} | |
create_log_dir() { | |
mkdir -p ${MONGO_LOG_DIR} | |
chmod -R 0755 ${MONGO_LOG_DIR} | |
chown -R ${MONGO_USER}:${MONGO_USER} ${MONGO_LOG_DIR} | |
} | |
map_uidgid | |
create_data_dir | |
create_log_dir | |
# allow arguments to be passed to mongod | |
if [[ ${1:0:1} = '-' ]]; then | |
EXTRA_ARGS="$@" | |
set -- | |
elif [[ ${1} == mongod || ${1} == $(which mongod) ]]; then | |
EXTRA_ARGS="${@:2}" | |
set -- | |
fi | |
# default behaviour is to launch mongod | |
if [[ -z ${1} ]]; then | |
echo "Starting mongod..." | |
exec start-stop-daemon --start --chuid ${MONGO_USER}:${MONGO_USER} \ | |
--exec $(which mongod) -- --config /etc/mongod.conf ${EXTRA_ARGS} | |
else | |
exec "$@" | |
fi | |
Now run the below command: | |
docker run --name mongodb -d restart=always --publish 27017:27017 --volume /srv/docker/mongodb:/var/lib/mongodb ajeetraina/mongodb:latest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment