MongoDB is a widely used NoSQL database that can function well in containers if you know how to set it up. Learn how to connect the Compass GUI here. Image: Continue Reading
MongoDB is a widely used NoSQL database that can function well in containers if you know how to set it up. Learn how to connect the Compass GUI here.
MongoDB is one of the most widely-used open source NoSQL databases on the market. It offers all the features you need to handle and manage massive troves of data and even offers an official desktop application that makes managing those databases a bit easier.
SEE: Hiring Kit: Database engineer (TechRepublic Premium)
You might think connecting the GUI application to a Docker-deployed instance of MongoDB would be pretty challenging, but it’s not nearly as hard as it sounds. In this tutorial, I’ll show you how to deploy the MongoDB container and then connect to it from MongoDB Compass.
What you’ll need to connect MongoDB Compass to a containerized database
To make this connection work, you’ll need a running instance of an operating system that supports both Docker and the MongoDB Compass app. I’ll demonstrate with Ubuntu Linux and show you how to install Docker, deploy the container and then connect Compass to a database. Please note that this process is compatible with a variety of Linux distros.
If you’re more interested in general instructions for installing MongoDB GUI Compass and connecting it to a remote server, this tutorial may be a better place to start.
Connecting to MongoDB hosted via Docker
The first thing to do when connecting to MongoDB through this method is installing Docker. You can add the official Docker GPG key with this command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Next, you’ll add the Docker repository:
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
From there, it’s time to install the necessary dependencies with this command:
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y
In order to install the latest version of the Docker engine, you can use the following two commands:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
Now, add your user to the Docker group with the following:
sudo usermod -aG docker $USER
Log out and log back in so the changes take effect.
Deploying and configuring the MongoDB container
We can now deploy the MongoDB container with the following:
docker run -d -p 27017:27017 --name example-mongo mongo:latest
With the container running, you’ll need to access it with this command:
docker exec -it example-mongo bash
Once inside the container, we need to edit the MongoDB configuration file with this command:
sudo nano /etc/mongod.conf.orig
In that file, locate the following section:
Change that section to the following:
Once you’ve made those changes, save and close the file. Exit from the container with the
From there, restart the container with:
docker restart ID
In that command, ID is the ID of the Mongo container. If you’re not sure of the ID, you can find it with:
Note: You might have to deploy the MongoDB container with environmental variables for the username and password, which can be done like so:
docker run -d –name some-mongo -e MONGO_INITDB_ROOT_USERNAME=NAME -e MONGO_INITDB_ROOT_PASSWORD=SECRET mongo
NAME is a username and SECRET is a unique and strong password.
Connecting to Compass
With the MongoDB container running, you can now connect to it with Compass using the same connect command you would use if MongoDB were installed via the traditional package manager and the user credentials you used with the environmental variables.
If you are still unable to connect to the containerized version of MongoDB from a remote instance of Compass, you might have to install Compass on the same machine running the MongoDB container.
Congratulations, you now have a well-designed GUI to help make your MongoDB admin tasks a bit easier. You can connect to as many MongoDB servers as you need to from Compass and start creating and managing all the MongoDB collections you need.
Read next: Top data quality tools (TechRepublic)