How to deploy MongoDB as a Docker container
Jack Wallen shows you how to set up a MongoDB container as a Docker container for your development needs.
MongoDB is an excellent NoSQL database that offers many features to meet the most demanding needs, but I’ve found that installing MongoDB on Linux distributions is a bit inconsistent. For example, MongoDB installs fine on Ubuntu 20.04, but there is no guarantee that it will start correctly. This is a problem I have 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 install of MongoDB? You could always go the container route. Finally, deploying with a container is a much more predictable route. In addition, it is significantly easier and you can run it on any computer that supports Docker.
This is a win-win situation. So if you need to get a MongoDB instance working for development purposes, read on.
What you need to deploy MongoDB as a container
The only things you need for this deployment are a computer that supports Docker and a user with sudo privileges. I will demonstrate on Ubuntu Server 22.04. Let us begin.
How to install Docker Community Edition
If you haven’t already installed Docker, here is the step to do so on Ubuntu Server. First you need to 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
Next, 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 some 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
Finally, 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.
How to deploy MongoDB with Docker?
Okay, it’s deployment time. Let’s get a version of MongoDB from the command line that I know works without problems. This command is:
docker pull mongo:3.4.4
Now before we run the mount command, we need to create a volume for the database so we can keep data in case something goes wrong with the container. Create the volume with:
docker volume create mongodata
Now that our volume is ready, we can mount it with the command:
docker run -d -v mongodata:/data/db --name mymongo --net=host mongo:3.4.4 --bind_ip 127.0.0.1 --port 27000
You can verify that the deployment was successful with the following command:
docker ps -a
You should see something like this in the output:
1a4dd5d216dc mongo:3.4.4 "docker-entrypoint.s…" 24 minutes ago Up 24 minutes mymongo
Once the container is running, you then need to know how to access it. This is actually quite simple. The command to access your running MongoDB container is:
docker exec -it mymongo mongo localhost:27000
You should then find yourself on the MongoDB console, where you can then start developing your databases. You can leave the console with the exit command and return to it with the above command.
If you want to stop the MongoDB container, you must first find its ID with the docker ps -a command, and then stop it with:
docker stop ID
Where ID is the ID associated with the MongoDB container. You can then start it again with:
docker start ID
And that’s all for deploying MongoDB as a container. This is a great way for database developers to quickly spin up a database server, work with it, and shut it down as needed.
Subscribe to TechRepublic’s How To Make Tech Work on YouTube for the latest tech advice for business professionals from Jack Wallen.