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
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:
curl –fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg –dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg |
The next step is adding the Docker repository, which can be achieved with:
echo “deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] & https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null |
Install the necessary dependencies with:
sudo apt–get install apt–transport–https ca–certificates curl gnupg lsb–release –y |
Update apt with:
sudo apt–get update |
Install the Docker Community Edition with:
sudo apt–get install docker–ce docker–ce–cli containerd.io –y |
Add your user to the Docker group with the command:
sudo usermod –aG docker $USER |
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:
mkdir –p ~/mongo/data |
With our directory to house persistent data ready, deploy the container with:
docker run –d —name example–mongo –v ~/mongo/data:/data/db –p 27017:27017 mongo:latest |
To verify the database container is running, issue the command:
You should see something like this in the output:
a30ddb85c456 mongo:latest “docker-entrypoint.s…” 5 days ago Up 5 days 27017/tcp, 0.0.0.0:27017–>27017/tcp, :::2701y–>2701y/tcp example–mongo |
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:
docker exec –it example–mongo bash |
You should now find yourself at the Bash prompt of the container, which will be denoted by something like this:
root@a30ddb85c456:/# |
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:
apt update |
Once apt is updated, install nano with:
apt–get install nano –y |
Open the MongoDB configuration file with the command:
nano /etc/mongod.conf.orig |
Locate the following section:
net: port: 27017 bindIp: 127.0.0.1 |
You must change that section to:
net: port: 27017 bindIp: 0.0.0.0 |
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:
net: port: 27017 bindIp: 192.168.1.62 |
Or, if you want to limit to all machines on your network, you could use something like the following:
net: port: 27017 bindIp: 192.168.1.0/24 |
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:
docker restart ID |
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:
mongodb://192.168.1.7:27017 |
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.
Created with Sketch.