Jack Wallen shows you how to use a MongoDB container as a Docker container for your development needs.
MongoDB is an excellent NoSQL database that offers plenty of features to meet the most demanding needs, but I’ve found that installing MongoDB is a bit inconsistent between Linux distributions. MongoDB installs just fine on Ubuntu 20.04, for example, but there is no guarantee that it will start correctly. That’s a problem I’ve encountered several times.
TO SEE: Rental kit: Database engineer (Tech Republic Premium)
What do you do when you don’t have time to install an installation of MongoDB and troubleshoot? You can always take the container route. After all, deploying with a container is a much more predictable route. Plus, it’s significantly simpler and you can run it on any machine that supports Docker.
That’s a win-win situation, so if you need a MongoDB instance for development purposes, read on.
What you need to deploy MongoDB as a container
All you need for this deployment is a machine that supports Docker and a user with sudo privileges. I’m going to demonstrate on Ubuntu Server 22.04. Let’s get down to business.
Install Docker Community Edition
If you have not already installed Docker, here is the step to do it on Ubuntu Server. The first thing to do is add the official Docker GPG key with:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Then add the official 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
Install a few dependencies with:
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y
Update apt with the command:
sudo apt-get update
Finally, install Docker with:
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
To wrap it up, make sure your user is a member of the docker group with the command:
sudo usermod -aG docker $USER
Log out and back in for the changes to take effect.
Deploy MongoDB with Docker
Get the latest Docker image from MongoDB with the command
docker pull mongo:latest
Before running the deploy command, we need to create a volume for the database so that we can keep data if something goes wrong with the container.
Create the volume with
docker volume create mongodata
Now that our volume is ready, we can deploy it with the command
docker run -d -v mongodata:/data/db --name mymongo --net=host mongo:latest --bind_ip 127.0.0.1 --port 27000
If a container is running, you need to know how to access it. That is actually very simple. The command to access your running MongoDB container would be
docker exec -it mymongo bash
Go to the mongoDB console with the command
You should be 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 with the exit command as well. You can then return to the MongoDB console with the previous commands when 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 don’t forget to like this video.