How to deploy MongoDB as a Docker container

How to deploy MongoDB as a Docker container

Jack Wallen shows you how to spin up a MongoDB container as a Docker container for your development needs.

Side profile low below angle view photo of cheerful positive cute pretty girl in checkered shirt debugging system of her corporation
Image; deagreez/Adobe Stock

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.

What you’ll need to deploy MongoDB as a container

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.

How to install Docker Community Edition

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:

curl -fsSL | 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] $(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 -y

To finish things up, make sure your user is a member of the docker group with the command:

sudo usermod -aG docker $USER

Log out and log back in so the changes take effect.

How do deploy MongoDB with Docker

Okay, it’s time to deploy. From the command line, let’s pull a version of MongoDB that I know works without issue. That command is:

docker pull mongo:3.4.4

Now, 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:

docker volume create mongodata

Now that our volume is ready, we can deploy with the command:

docker run -d -v mongodata:/data/db --name mymongo --net=host mongo:3.4.4 --bind_ip --port 27000

You can verify the deployment was successful with the 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

With the 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:

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 exit the console with the exit command and return to it with the command above.

If you want to stop the MongoDB container, you’d first need to find the associated ID, using the command docker ps -a, and then stop it with:

docker stop ID

Where ID is the ID associated with the MongoDB container. You could then start it back up with:

docker start ID

And that’s all there is to 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 all the latest tech advice for business pros from Jack Wallen.

Source of Article