Skip to content

Instantly share code, notes, and snippets.

@nikcorg
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikcorg/53766f19c725659577dc to your computer and use it in GitHub Desktop.
Save nikcorg/53766f19c725659577dc to your computer and use it in GitHub Desktop.
Docked MongoDB helpers

Some scripts for working with Boot2Docker and the Docker mongodb image (https://registry.hub.docker.com/u/dockerfile/mongodb/)

The scripts

mongo-cli.sh opens an interactive mongo shell

mongo-restore.sh will fetch a zip, unpack it and mongorestore a contained dump

prompt.sh will open an interactive shell in the running docker instance

start-mongod.sh will start a docker instance. When running for the first time, the image will be pulled, unless you already have it.

stop-mongod.sh will stop a running docker instance and clean up

remove-mongod.sh will remove the mongodb docker image completely

env.sh does some environment checks and setup

# Make sure boot2docker is installed
if [ "x$(which boot2docker)" = "x" ];
then
echo "Can't find boot2docker"
exit 1
fi
# Make sure boot2docker is running
if [ "$(boot2docker status)" != "running" ];
then
echo "Looks like boot2docker isn't running"
exit 1
fi
# Check and set the DOCKER_HOST environment variable
if [ "x$DOCKER_HOST" = "x" ];
then
$(boot2docker shellinit)
fi
# Paranoia check
if [ "x$DOCKER_HOST" = "x" ];
then
echo "Unable to find or set DOCKER_HOST enviroment variable. Set manually and rerun"
exit 1
fi
require_mongodb () {
ID=$(docker ps -a | grep mongodb | grep -v "Exit" | awk '{print $1}')
if [ "x$ID" = "x" ];
then
echo "Looks like mongodb isn't running"
exit 1
fi
}
#!/bin/bash
source env.sh
require_mongodb
docker run -it --rm --link mongodb:mongodb dockerfile/mongodb bash -c 'mongo --host mongodb'
#!/bin/bash
# Use this script to fetch a zip from the M101 class in MongoDB University and restore it to the running instance
# Pass the URL to the zip-file as a parameter
source env.sh
require_mongodb
docker run -it --rm --link mongodb:mongodb dockerfile/mongodb bash -c 'cd /tmp && curl -O "'$1'" && unzip *.zip && (mongorestore --host mongodb $(dirname $(dirname $(find . -type f -name "*.bson"|grep -v "__MACOSX"|head -1))))'
#!/bin/bash
source env.sh
require_mongodb
docker run -it --rm --link mongodb:mongodb dockerfile/mongodb bash
#!/bin/bash
source env.sh
# Stop the service
./stop-mongod.sh
# Remove the image
docker images --no-trunc | grep mongodb | awk '{print $3}' | xargs docker rmi >/dev/null 2>&1
#!/bin/bash
source env.sh
ID=$(docker ps -a | grep mongodb | grep -v "Exit" | awk '{print $1}')
if [ "x$ID" != "x" ];
then
echo "Looks like mongodb is already running"
exit 1
fi
docker run -d -p 27017:27017 -p 28017:28017 --name mongodb dockerfile/mongodb mongod --rest --httpinterface >/dev/null 2>&1
#!/bin/bash
source env.sh
ID=$(docker ps | grep mongodb | awk '{ print $1 }')
if [ "x$ID" != "x" ];
then
# Stop the running process
docker stop $ID >/dev/null 2>&1
fi
# Clean up (remove) the stopped containers
docker ps -a --no-trunc | grep 'Exit' | grep mongodb | awk '{print $1}' | xargs docker rm >/dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment