Setting Up a MongoDB Server on Your Computer Using Docker

Setting Up a MongoDB Server on Your Computer Using Docker

by Jason Lewis on Aug 17, 2019

SITE NAME CHANGE: Hi there, this is just a quick note to explain why the site name seems to have changed. I've decided that my personal domain of Gecko8 wasn't nearly descriptive enough. I want to cover not just code, but how to make our lives better as developers. With that goal in mind, I feel Life And Code is a better choice. I hope you agree!

Have you ever been working through a tutorial on the latest server technology and gotten to the part about connecting it to a database, only to stall trying to setup the database server so you can play with it? Maybe gotten lost in a sea of configuration options where you don’t know what half the words mean, let alone where best to put such and such files? I certainly have (here’s looking at you SQL Server). And don’t get me started on trying to remove it once you’re finished! I’ll be honest, there have been times I’ve just moved on to learn something else rather than beat myself up trying to figure it out.

Fortunately, along came Docker to show us a better way. Now when I want to setup a database server I can usually set it up in Docker in a matter of minutes and get on with what I’m trying to accomplish. And when I’m done, a quick command line can make it all go away!

Recently, I’ve been learning the Node.js stack with MongoDB. Mongo is perfectly suited for running in a Docker container. Here’s how to set it up!

Note: This guide assumes you have Docker installed and running. If you want to confirm this, just enter the command:

docker -v

As long as you see something like (version/build number may be different)

Docker version 19.03.1, build 74b1e89

You should be good to go.

Installing the MongoDB Container

In it’s most basic setup, the following command will start up a MongoDB container which stores it’s database information internally.

docker run -d -p 27017:27017 --name <container name> mongo

Or if you want a specific version

docker run -d -p 27017:27017 --name <container name> mongo:<version>

Simply put, this gets the MongoDB Docker image (mongo), creates a container from it on your local Docker instance, and runs it. It makes the default MongoDB client port available on port 27017 so you can easily connect to it from a client application. The -d switch runs it in daemon mode which frees up your terminal for other commands.

This has a downside however. Your database data is stored inside the docker container so if you delete the container, your data goes away with it. There are two possible solutions for this.

1. Store Your Data In a Docker Volume

With this method, removing the container won’t touch your data. This has the benefit that it requires no access to the server computer's file system. The downside is if you need access to the files for some reason, it’s more involved. You accomplish this with the -v parameter containing a volume name (anything you like) and the MongoDB database data location (/data/db).

docker run -d -p 27017:27017 -v <volume name>:/data/db --name <container name> mongo

2. Store Your Data in a Folder on Your File System

This technique makes your data the most accessible but can occasionally lead to permission issues depending on where you want to put the files. I run a Macbook Pro for development and typically store all my database files in a /Data/Databases folder off the root. This method has worked well for me. This method also uses the -v parameter, but uses a path on your system instead of a volume name.

docker run -d -p 27017:27017 -v <location on your drive>:/data/db --name <container name> mongo

That’s really all there is to it! You can now connect to your database server as usual on localhost:27017.