-
-
Save flyhigher139/7a2ebbd85b1943a5dd60bdfc1b531bbc to your computer and use it in GitHub Desktop.
For setup Docker container and use data volume for db.
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
#!/bin/bash | |
CONTAINER_INDEX=$1 | |
CONTAINER_NAME="mongo"$1 | |
DATA_VOLUME="mongo-data-volume"$1 | |
mkdir $CONTAINER_NAME | |
DB_PORT=$((30000+$CONTAINER_INDEX)) | |
HTTP_PORT=$((28017+$CONTAINER_INDEX)) | |
NETWORK_NAME=$2 | |
REPLICASET_NAME=$3 | |
# Set oplog size. | |
oplogSizeMB=128 | |
# Remove old container if has. | |
docker rm $CONTAINER_NAME -f | |
# Remove old folder if has. | |
rm -rf $CONTAINER_NAME | |
# Create log container. | |
mkdir -p $CONTAINER_NAME/var/log/mongodb | |
# Remove old data volume container if has. | |
docker rm $DATA_VOLUME -f | |
# Create data volume. | |
docker create --name $DATA_VOLUME -v /data/db mongo /bin/true | |
# Export config. | |
cat <<EOF > $CONTAINER_NAME/mongo.conf | |
systemLog: | |
destination: file | |
path: "/var/log/mongodb/mongod.log" | |
logAppend: true | |
storage: | |
mmapv1: | |
smallFiles: true | |
net: | |
http: | |
enabled: true | |
JSONPEnabled: true | |
RESTInterfaceEnabled: true | |
setParameter: | |
enableLocalhostAuthBypass: true | |
replication: | |
oplogSizeMB: $oplogSizeMB | |
replSetName: $REPLICASET_NAME | |
EOF | |
docker run -d \ | |
-p $DB_PORT:27017 \ | |
-p $HTTP_PORT:28017 \ | |
--name $CONTAINER_NAME \ | |
--net $NETWORK_NAME \ | |
-v $PWD/$CONTAINER_NAME/var/log/mongodb:/var/log/mongodb \ | |
--volumes-from $DATA_VOLUME \ | |
-v $PWD/$CONTAINER_NAME:/$CONTAINER_NAME \ | |
mongo mongod --config /$CONTAINER_NAME/mongo.conf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment