MongoDB on Windows in Minutes with Docker
A fast, easy way to get up and running with NoSQL on your local machine.
Recently, I was working on a demo for an upcoming series of talks that required a (preferably local) instance of MongoDB. If you arenât familiar with MongoDB, itâs a very popular and mature document database. The API for MongoDB is supported by Azure Cosmos DB and the demo illustrates how to migrate from a local instance of MongoDB for development purposes to a cloud-based production instance of Cosmos DB. There was just one catch: I had no desire to install MongoDB on my Windows 10 machine!
Note: although Iâm focusing on Windows-based steps, the same steps should work fine on macOS and Linux.
Fortunately, this is precisely the type of scenario Docker was designed to address. Using containers, you can quickly get up and running with any number of predefined images and services. Mongo maintains a set of official Docker images.
I assume you have Docker installed and are using Linux (not Windows) containers. I am running a Windows-specific version of desktop for community.
Now that the prerequisites are out of the way, there are two steps to getting MongoDB up and running. First, create a volume to persist data between runs. If you skip this step, any changes you make will disappear when the container stops running.
docker volume create --name=mongodata
The name can be anything you like. The next step will pull the database image if it doesnât already exist, then launch a running instance using the mounted volume.
docker run --name mongodb -v mongodata:/data/db -d -p 27017:27017 mongo
You can give the running container any name you like. The first time may feel like npm install
as multiple layers are downloaded, but subsequent runs should go quite fast.
Thatâs it. Youâre done. You have a fully functional version of MongoDB running on your machine! (Hereâs mine running, mapped to a different port).
Of course, you probably want to tweak it a bit. By default, itâs running without authentication. To set up authentication, you need to create a login and then restart the service with the âauthenticationâ switch.
First, log into the running (non-authenticated) version.
winpty docker exec -it mongodb bash
(winpty
is needed from a typical Windows command line. You can omit it to run from other terminals. There is nothing special about mongodb
other than itâs the name I gave the container in the previous step).
Open the MongoDB terminal:
mongo
Letâs assume your database is called âmydatabaseâ and you want to set up a user named âmyuserâ with password âsecretâ. These two steps will take care of it for you (the database does not have to exist yet):
use mydatabase
db.createUser({user:"myuser", pwd:"secret", roles:[{role:"readWrite", db: "mydatabase"}]});
After that, you can exit out of the MongoDB terminal and the bash shell thatâs running. Next, stop and remove the existing instance and launch a new one with authentication active:
docker stop mongodb
docker rm mongodb
docker run --name mongodb -v mongodata:/data/db -d -p 27017:27017 mongo --auth
Now you can authenticate with the connection string:
mongodb://myuser:secret@localhost:27017/mydatabase
Thatâs it! Hopefully this simple set of steps is helpful for you to get up and running with MongoDB on your machine.
Regards,
Related articles:
- Managing Data đ in the Cloud â (Mongodb)
- My New Role as Senior Program Manager for .NET Data (NoSQL)
- NoSQL and Azure Cosmos DB in 20 Minutes (Cosmosdb)
- Say âYesâ to NoSQL for .NET SQL Developers (Mongodb)
- Using LINQ to Query Dynamic Schema-less Cosmos DB Documents (NoSQL)
- What the Shard? (Cosmosdb)