Script to up MongoDB in docker container and init it with some schema and test data
Last active
May 8, 2018 18:21
-
-
Save smikheiev/27b3ac7ef45884458e2f7fc69ee814cc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
db.fsyncLock({fsync: 1}) | |
db.fsyncUnlock() |
This file contains hidden or 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
use testDb | |
db.createCollection("testCollection") |
This file contains hidden or 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/zsh | |
echo -e "> I am: `whoami`" | |
echo -e "> I am in: `pwd`" | |
CONTAINER_NAME="container-name" | |
MONGO_IMAGE="mongo:latest" | |
if [ "$(docker ps -a | grep ${CONTAINER_NAME})" ]; then | |
echo -e "\n> Container '${CONTAINER_NAME}' is running. Stop and remove it..." | |
docker stop ${CONTAINER_NAME} | |
docker rm ${CONTAINER_NAME} | |
fi | |
echo -e "\n> Pull mongo image '${MONGO_IMAGE}'" | |
docker pull ${MONGO_IMAGE} | |
echo -e "\n> Run mongo container as '${CONTAINER_NAME}'" | |
docker run -d -p 27017:27017 --name ${CONTAINER_NAME} -d ${MONGO_IMAGE} mongod | |
docker cp ./initDbSchema.js ${CONTAINER_NAME}:/initDbSchema.js | |
docker cp ./putTestData.js ${CONTAINER_NAME}:/putTestData.js | |
docker cp ./flushDb.js ${CONTAINER_NAME}:/flushDb.js | |
echo -e "\n> Initialize db" | |
docker exec ${CONTAINER_NAME} /bin/bash -c "mongo < ./initDbSchema.js" | |
docker exec ${CONTAINER_NAME} /bin/bash -c "mongo < ./putTestData.js" | |
docker exec ${CONTAINER_NAME} /bin/bash -c "mongo < ./flushDb.js" | |
echo -e "\n> Well done, mate ;)" |
This file contains hidden or 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
use testDb | |
db.testCollection.insertOne({ | |
"testKey": "test value" | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment