Getting Started With MongoDB As A Docker Container Deployment

Getting Started With MongoDB As A Docker Container Deployment

In this tutorial we’re going to focus on using Docker to deploy MongoDB as a container and interact with it with the shell client.

In this tutorial we’re going to focus on using Docker to deploy MongoDB as a container and interact with it with the shell client.

MongoDB is one of the most popular NoSQL databases on the market right now and is used heavily with Node.js development in particular. So what if you wanted to give MongoDB a spin and see what it’s all about?

There are plenty of deployment options when it comes to using MongoDB. For example, I had recently written a tutorial titled, Developing a RESTful API with Node.js and MongoDB Atlas which focused on the MongoDB cloud solution called Atlas. However, you can also install MongoDB on premise using multiple options.

Downloading the Latest MongoDB Docker Image

Assuming that you have Docker installed on your computer or server, we can obtain the MongoDB image from the Docker Hub container registry. There are numerous images available, each representing different versions of MongoDB or the underlying operating system that it is installed on.

The simplest solution to download our image is to execute the following:

docker pull mongo

The above command will get the latest tag which could be anything. This is great if you want to be bleeding edge, but when you’re doing realistic deployments you probably want to be in control of the versioning information. Instead, you might want to specify the tag like this:

docker pull mongo:4.0.4

At the time of writing this, MongoDB 4.0.4 is the latest version and it is what the latest tag represents. This time we’re actually specifying the 4.0.4 tag because that is what we want.

It may take a moment, but when it is done, we have an image that is ready for deployment.

Deploying an Instance of MongoDB as a Container

With the image available to us, we need to deploy it. In its simplest form, and what is outlined in the Docker Hub documentation, we can execute the following command:

docker run --name mongodb mongo:4.0.4

This command works, but there are potentially a few problems. What we’re doing is we are running a container and naming it mongodb. We aren’t running it in the background and if we wanted to connect to it, we’d have to do it from another container instance. In other words, we wouldn’t be able to connect to it from our host computer or server.

Instead, what we could do is the following:

docker run -d -p 27017-27019:27019 --name mongodb mongo:4.0.4

The above command will run the container in detached mode, or in the background for us. We are also mapping the container ports with host ports so that way we can access the database from a host level application if we wanted to. The ports used were taken from the MongoDB documentation. You don’t need to map the ports in order to use MongoDB. Like I said, the port mapping is only necessary if you wish to use it from your host Mac, Windows, or Linux computer. If you plan to deploy all your applications as micro-services with Docker, then you’d be fine as long as your containers can communicate with each other.

Interacting with the MongoDB Docker Container with Basic Shell Operations

At this point in time we have a functional MongoDB deployment. Rather than creating an application to make use of it, we’re going to interact with the database through the shell client.

If you’ve been following along so far, your container is currently running in detached mode. This means that we need to connect to it using the interactive terminal:

docker exec -it mongodb bash

The above command will connect to our deployment named mongodb using the interactive terminal and start the bash shell. More details on connecting to detached Docker containers can be found in my previous tutorial titled, Connecting to a Detached Docker Container for Terminal Interaction.

You’ll notice that you are now using your Terminal as if you were inside your container. This is where we can start using MongoDB.

To launch the MongoDB shell client, execute the following:

mongo

When inside the MongoDB shell client, you can access all the functionality that is outlined in the MongoDB documentation. For example, we can see what databases exist in our instance with the following:

show dbs

To create a new database, we can use a multi-step process, the first being to define the database we wish to use:

use thepolyglotdeveloper

We’re using the database thepolyglotdeveloper, but it doesn’t exist until we start creating collections and data. To create a collection with data, we can do something like this:

db.people.save({ firstname: "Nic", lastname: "Raboy" }) db.people.save({ firstname: "Maria", lastname: "Raboy" })

With two documents created in a new people collection in our thepolyglotdeveloper database, we can query for data using something like the following:

db.people.find({ firstname: "Nic" })

There is a lot that you can accomplish with the shell client, but you can get the general idea. What we did prove is that we were able to interact with the container instance.

Conclusion

You just saw how to download and deploy a Docker image of MongoDB as a container, then interact with it using the shell client. Docker isn’t the only way to deploy MongoDB and the shell client isn’t the only way to interact with it. These are just two of many examples on what you can accomplish.

If you want to learn more about Docker, consider enrolling in my video course titled, Containerization with Docker by Example.

A video version of this article can be seen below.

MongoDB Tutorial - How to backup and restore a database

MongoDB Tutorial - How to backup and restore a database

This is a quick example to show how to easily backup and restore a MongoDB database from the command line, using the current date as the backup folder name (YYYYMMDD)

Originally published at https://jasonwatmore.com
Backup MongoDB database using date for the folder name

The mongodump command will create a backup / dump of the MongoDB database with the name specified by the --db [DB NAME] argument.

The --out /var/backups/`date +"%Y%m%d" argument specifies the output directory as /var/backups/[TODAY'S DATE] e.g. /var/backups/20190903.

sudo mongodump --db [DB NAME] --out /var/backups/`date +"%Y%m%d"`
Restore MongoDB database from backup

The mongorestore command restores a database to the destination --db [DB NAME] from the specified directory, e.g. /var/backups/20190903.

sudo mongorestore --db [DB NAME] /var/backups/[BACKUP FOLDER NAME]

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Learn More

MongoDB - The Complete Developer’s Guide

The Complete Developers Guide to MongoDB

Learn MongoDB : Leading NoSQL Database from scratch

Learn NoSQL Databases - Complete MongoDB Bootcamp 2019

MongoDB Connection Initialization – Node.js API with TDD Tutorial

Building a simple app using NodeJS, MongoDB, and ExpressJS

Node, Express, React.js, Graphql and MongoDB CRUD Web Application

Building REST API with Nodejs / MongoDB /Passport /JWT

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB - Part 3/3

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB

What you’ll learn

  • Work with MongoDB with Clarity and Confidence
  • Use 4 tools MongoCHEF, NOSQL Manager, RoboMongo, MongoBooster easily
  • Do Regex, GridFS , Replication , Sharding, Full text search
  • Basic and Advanced CRUD operations using MongoDB
  • Import and Export data from MongoDB
  • Work MapReduce, Embedded Documents,Save&Insert , indexing, capped collections, TTL
  • Bonus section * Use java,C#,PHP,Nodejs to access MongoDB features like CRUD, GridFS
  • Bonus Section * A 50 minutes MongoDB key feature exercises
  • 100+ Quizzes 40+ Activities

Learn More

MongoDB - The Complete Developer’s Guide

The Complete Developers Guide to MongoDB

MongoDB - The Complete Developer’s Guide

Learn MongoDB : Leading NoSQL Database from scratch

Learn NoSQL Databases - Complete MongoDB Bootcamp 2019

Build a CRUD Operation using PHP & MongoBD

MongoDB with Python Crash Course - Tutorial for Beginners

Learn NoSQL Databases from Scratch - Complete MongoDB Bootcamp 2019

AngularJS tutorial for beginners with NodeJS, ExpressJS and MongoDB

MEAN Stack Tutorial MongoDB, ExpressJS, AngularJS and NodeJS

Creating RESTful APIs with NodeJS and MongoDB Tutorial

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB - Part 1/3

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB

What you’ll learn

  • Work with MongoDB with Clarity and Confidence
  • Use 4 tools MongoCHEF, NOSQL Manager, RoboMongo, MongoBooster easily
  • Do Regex, GridFS , Replication , Sharding, Full text search
  • Basic and Advanced CRUD operations using MongoDB
  • Import and Export data from MongoDB
  • Work MapReduce, Embedded Documents,Save&Insert , indexing, capped collections, TTL
  • Bonus section * Use java,C#,PHP,Nodejs to access MongoDB features like CRUD, GridFS
  • Bonus Section * A 50 minutes MongoDB key feature exercises
  • 100+ Quizzes 40+ Activities


Learn More

MongoDB - The Complete Developer’s Guide

The Complete Developers Guide to MongoDB

MongoDB - The Complete Developer’s Guide

Building A REST API With MongoDB, Mongoose, And Node.js

Node.js, ExpressJs, MongoDB and Vue.js (MEVN Stack) Application Tutorial

MEAN Stack Tutorial MongoDB, ExpressJS, AngularJS and NodeJS

MongoDB with Python Crash Course - Tutorial for Beginners