How To Deploy MongoDB As A Docker Container
- by Tech Today News
- Posted on March 11, 2023
Jack Wallen shows you how to spin up a MongoDB container as a Docker container for your development needs. MongoDB is an outstanding NoSQL database that offers plenty of features to satisfy the most demanding needs, but I’ve found installing MongoDB to be a bit inconsistent across Linux distributions. MongoDB might install just fine on, say, Ubuntu 20.04, but there’s no guarantee it will start properly. That’s an issue I’ve experienced on several occasions. SEE: Hiring Kit: Database engineer (TechRepublic Premium) What do you do when you don’t have time to install and troubleshoot an installation of MongoDB? You could always go the container route. After all, deploying with a container is a much more predictable route. On top of that, it’s considerably easier and you can spin it up on any machine that supports Docker. That’s a win-win, so if you need to get a MongoDB instance up and running for development purposes, read on. The only things you’ll need for this deployment are a machine that supports Docker and a user with sudo permission. I’m going to demonstrate on Ubuntu Server 22.04. Let’s get to it. In case you don’t already have Docker installed, here is the step to do so on Ubuntu Server. The first thing to do is add the official Docker GPG key with:
Next, add the official Docker repository:
Install a few dependencies with:
Update apt with the command:
Finally, install Docker with:
To finish things up, make sure your user is a member of the docker group with the command:
Log out and log back in so the changes take effect. Pull the latest Docker image from MongoDB with the command
Before we run the deployment command, we need to create a volume for the database so we can retain data should something go awry with the container. Create the volume with
Now that our volume is ready, we can deploy it with the command
With a container running, you will then need to know how to access it. That’s actually quite simple. The command to access your running MongoDB container would be
Access the mongoDB console with the command
You should find yourself on the MongoDB console, where you can start developing your databases. You can exit the console with the exit command, and then exit the container also with the exit command. You can then return to the MongoDB console with the previous commands whenever it’s time to work with the database again. For more tutorials from Jack Wallen, subscribe to TechRepublic’s YouTube channel How To Make Tech Work — and remember to like this video.What you’ll need to deploy MongoDB as a container
How to install Docker Community Edition
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
How to deploy MongoDB with Docker
docker pull mongo:latest
docker volume create mongodata
docker run -d -v mongodata:/data/db --name mymongo --net=host mongo:latest --bind_ip 127.0.0.1 --port 27000
docker exec -it mymongo bash
mongosh localhost:27000
Jack Wallen shows you how to spin up a MongoDB container as a Docker container for your development needs. MongoDB is an outstanding NoSQL database that offers plenty of features to satisfy the most demanding needs, but I’ve found installing MongoDB to be a bit inconsistent across Linux distributions. MongoDB might install just fine on,…