How To Connect The Compass GUI To A Docker-Deployed MongoDB Database
- by Tech Today News
- Posted on February 8, 2023
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. Jump to: 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. 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:
Next, you’ll add the Docker repository:
From there, it’s time to install the necessary dependencies with this command:
In order to install the latest version of the Docker engine, you can use the following two commands:
Now, add your user to the Docker group with the following:
Log out and log back in so the changes take effect. We can now deploy the MongoDB container with the following:
With the container running, you’ll need to access it with this command:
Once inside the container, we need to edit the MongoDB configuration file with this command:
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:
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. 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)What you’ll need to connect MongoDB Compass to a containerized database
Connecting to MongoDB hosted via Docker
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
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
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
sudo usermod -aG docker $USER
Deploying and configuring the MongoDB container
docker run -d -p 27017:27017 --name example-mongo mongo:latest
docker exec -it example-mongo bash
sudo nano /etc/mongod.conf.orig
net:
port: 27017
bindIp: 127.0.0.1
net:
port: 27017
bindIp: 0.0.0.0
exit
command.docker restart ID
docker ps
Connecting to Compass
Next steps
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: Timon/Adobe Stock MongoDB is one of the most widely-used open source NoSQL databases on the market. It offers all the features you need to handle…