Tutorial: Setting up MongoDB Image Instance with Docker Toolbox

This tutorial will show you how to get a MongoDB image instance up and running with Docker Toolbox.

Update: Since this tutorial was published, Docker introduced Kitematic, part of the Docker Toolbox to help with setting up Local Databases. Continue reading to see how to do this via Docker command line tools.

Getting an instance of MongoDB up and running with Docker Toolbox is both easy and provides some nice benefits. Docker gives you the benefit of knowing that your code base will operate on any OS that supports Docker, which is quite extensive at this point. Also, as your code scales Docker will empower you to install more MongoDB instances on multiple servers to handle load quite simply. Lets dive into getting your MongoDB instance running with Docker Toolbox.

Docker ToolBox

Docker Toolbox is a handy sandbox environment that the creators of Docker have made to help you get started with Docker easily. Basically it spins up a VM (Virtual Machine) on your host machine (OSX or Windows) with Docker Installed. Instructions can be found along with the necessary downloads below.

https://www.docker.com/toolbox

After you have Docker Toolbox installed just use the Docker QuickStart Terminal and it will spin up your Docker environment and launch a terminal for you.

MongoDB Image

Next we need to pull down the MongoDB Docker image we will be using for our instance. We will be using the official mongo image found on DockerHub here. In the Docker Toolbox terminal window use the following command.

docker pull mongo

After the image downloads we can now start up our MongoDB container using the image. Run the following command to start up your MongoDB instance.

docker run --name database -d -p 27017:27017 mongo --noauth --bind_ip=0.0.0.0

Your MongoDB Docker instance should be up and running now. You can verify this with the following command.

docker ps

You should see a list of running Docker containers. In this case your mongoDB container.

alt

Your mongoDB instance is now up and running. In the next two section I'm going to show you how you can forward ports to your host machine in Virtual Box and connect to your mongoDB Docker container via the Robomongo client.

Virtual Box

When we started the mongoDB container with Docker we bound the containers port to the Docker Toolbox VM port of 27017 (standard mongo port) with the argument -p 27017:27017. Now we need to forward any port on our host machine to the Docker Toolbox VM. I'm going to use port 1111, but you can use whatever you like.

  • Open up VirtualBox.
  • Right click the "Default" machine.
  • Select settings from the menu.
  • Choose the Network tab in the settings menu.
  • Choose port forwarding at the bottom of the network option.
  • Click the plus icon to the right of the port forwarding menu.
  • Make the new entry with the name mongo.
  • Protocol should be TCP.
  • Host and Guest IP can be left empty.
  • Set the host port to whatever you want, I used 1111.
  • Set the guest port to 27017.

RoboMongo Client

You can download RoboMongo at http://robomongo.org/download.html

After you have downloaded and installed RoboMongo click on the connection button on the top left of the application. It will look like two computers depending on what version you have. Choose to create a new connection. Name your connection and set the address to localhost or 127.0.0.1 and the port to the port you set your Host port too in VirtualBox, in my case 1111. Then simply connect and start adding databases and collections to your mongoDB instance!


I hope this tutorial helps to get you set up your own MongoDB Image Instance using Docker Toolbox. If you have any questions or want to share your experience, please leave a comment below!


About Author: Keith Keough
Keith Keough

full stack dev, technology enthusiast