VOOZH about

URL: https://thenewstack.io/deploy-mongodb-in-a-container-access-it-outside-the-cluster/

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


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2023-02-18 07:05:20
Deploy MongoDB in a Container, Access It Outside the Cluster
Containers / Data / Kubernetes

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 Jack Wallen
👁 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:

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:

docker ps

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:

docker ps

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.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
Docker and MongoDB are sponsors of The New Stack.
TNS owner Insight Partners is an investor in: Docker.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.