Deploy MongoDB in a Container, Access It Outside the Cluster – The New Stack

MMS Founder
MMS RSS

Posted on nosqlgooglealerts. Visit nosqlgooglealerts

<meta name="x-tns-categories" content="Containers / Data / Kubernetes“><meta name="x-tns-authors" content="“>

Deploy MongoDB in a Container, Access It Outside the Cluster – The New Stack

Modal Title

2023-02-18 07:05:20

Deploy MongoDB in a Container, Access It Outside the Cluster

How to a deploy a containerized version of MongoDB and connect to it from a machine or service outside of the hosting server.


Feb 18th, 2023 7:05am by


Featued image for: Deploy MongoDB in a Container, Access It Outside the Cluster

Image by Jiawei Zhao on Unsplash.

At some point, during your journey with container development, you’ll have to deploy a database to serve as a data storage facility for your applications. Or maybe you plan on using a NoSQL database for the purpose of analytics and you rather deploy that database as a container, for its convenience.

That’s part of the beauty of Docker containers … they’re simple to deploy and use. Consider MongoDB. I’ve had numerous instances (especially when deploying a newer version to my go-to server distribution, Ubuntu), where the mongod service simply won’t start.

However, if I deploy that database as a container, I rarely (if ever) have problems. I can deploy that MongoDB container in seconds and connect to it from outside the cluster.

Hold up a moment. If you’re savvy enough, you know that it’s not so simple. No matter how you deploy MongoDB, the default configuration doesn’t allow connection from anywhere but localhost. So how in the world would you be able to deploy a containerized version of MongoDB and connect to it from a machine or service outside of the hosting server?

There’s a trick to that.

Now, I’m not saying this is the ideal method of using MongoDB as a container. What it does offer, however, is a really good insight into how Docker containers can be used and I’m going to show you how.

Are you ready for this?

Requirements

In order to successfully pull this off, you’ll need an operating system that supports Docker. I’ll demonstrate on Ubuntu Server 22.04, but you can use the platform of your choice. The only thing you’ll have to alter is the installation process for Docker (because I’m going to show you how to take care of that as well).

Let’s do it.

Installing Docker

In the name of not making you read yet another article, let me show you how to install Docker on Ubuntu Server. It’s actually quite simple…just cut and paste the following commands into the Linux terminal window.

To begin with, you must add the official Docker GPG key with the following command:

The next step is adding the Docker repository, which can be achieved with:

Install the necessary dependencies with:

Update apt with:

Install the Docker Community Edition with:

Add your user to the Docker group with the command:

Log out and log back in so the changes take effect.

Outstanding work so far.

Deploy the MongoDB Container

It’s time to deploy the MongoDB container. Just for fun, let’s deploy the container with persistent storage. First, we’ll create a directory on the hosting machine to house the data with the command:

With our directory to house persistent data ready, deploy the container with:

To verify the database container is running, issue the command:

You should see something like this in the output:

Congratulations, your database container has been deployed and is ready for configuration.

Configuring the Database for External Connections

At this point, the container is up and running, but only accessible from within localhost. In other words, you can’t reach it from outside the container. Fortunately, this fix is rather simple. First, you must know how to access the Bash prompt of a running container. Remember, when we deployed our container above, we named it example-mongo with the option --name example-mongo. We’ll use that name to access the container with the command:

You should now find yourself at the Bash prompt of the container, which will be denoted by something like this:

Before we do anything, let’s install the nano text editor (because it’s far easier to use than the included vi. To do that, update apt with:

Once apt is updated, install nano with:

Open the MongoDB configuration file with the command:

Locate the following section:

You must change that section to:

The above configuration opens MongoDB to any connection. If you want to limit to a specific IP on your network, you could configure it with something like this:

Or, if you want to limit to all machines on your network, you could use something like the following:

Save and close the file with the keyboard combination [Ctrl]+[X]. Exit from the container with the exit command. Since we’ve made changes to the database configuration, the easiest method of restarting MongoDB is to simply restart the container itself. To do that, we first must locate the container ID, which is done with the command:

You’ll only need the first four characters of the container ID. With those in hand, restart the container with:

Where ID is the ID of the example-mongo container. If you then access the container’s Bash prompt again, you can view the MongoDB configuration file and see your changes are still intact.

One thing I’ve found to be a big help with MongoDB is installing the MongoDB Compass app, which is a GUI tool for managing your databases. With that installed, you can connect to the containerized MongoDB with a connection string like:

From within Compass, you can create/edit databases/collections.

At this point, you can now access your MongoDB container from beyond the host file, which means you can use that database for a number of purposes. But, more than anything, you’ve learned how to install Docker, deploy a container with persistent storage, access the container, configure a service, and restart the container.

I would also be remiss if I didn’t mention this isn’t exactly the most secure method of using MongoDB. After all, you’d want to create a database user with access to a specific database, but that’s outside of the scope of this piece. However, if you know how to use MongoDB, you already know how to create an authenticated user.

Even so, this is a great way to learn the ins and outs of deploying a Docker container.

Group
Created with Sketch.

TNS owner Insight Partners is an investor in: Docker.

Subscribe for MMS Newsletter

By signing up, you will receive updates about our latest information.

  • This field is for validation purposes and should be left unchanged.