How to deploy MongoDB as a Docker container

Uncategorized

Jack Wallen reveals you how to spin up a MongoDB container as a Docker container for your development

requires. MongoDB is an exceptional NoSQL database that uses lots of functions to satisfy the most demanding requirements, however I have actually found setting up MongoDB to be a bit irregular throughout Linux distributions. MongoDB might install just fine on, say, Ubuntu 20.04, however there’s no warranty it will begin properly. That’s an issue I have actually experienced on several events.

SEE: Hiring Kit: Database engineer (TechRepublic Premium)

What do you do when you don’t have time to install and fix an installation of MongoDB? You might always go the container path. After all, releasing with a container is a far more foreseeable route. On top of that, it’s significantly much easier and you can spin it up on any device that supports Docker.

That’s a win-win, so if you need to get a MongoDB circumstances up and running for development functions, read on.

What you’ll need to release MongoDB as a container

The only things you’ll need for this release are a maker that supports Docker and a user with sudo consent. I’m going to demonstrate on Ubuntu Server 22.04. Let’s get to it.

How to set up Docker Neighborhood 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 include the main 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 main 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 reliances with:

sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y

Update apt with the command:

sudo apt-get upgrade

Finally, install Docker with:

sudo apt-get set up docker-ce docker-ce-cli containerd.io -y

To end up things up, make sure your user belongs to the docker group with the command:

sudo usermod -aG docker $USER

Log out and log back in so the changes work.

How to release MongoDB with Docker

Pull the most recent Docker image from MongoDB with the command

docker pull mongo: most current

Before we run the deployment command, we require to develop a volume for the database so we can retain information need to something go awry with the container.

Create the volume with

docker volume produce mongodata

Now that our volume is all set, we can deploy it with the command

docker run -d -v mongodata:/ data/db– name mymongo– internet=host mongo: latest– bind_ip 127.0.0.1– port 27000

With a container running, you will then need to know how to access it. That’s in fact quite simple. The command to access your running MongoDB container would be

docker exec -it mymongo bash

Gain access to the mongoDB console with the command

mongosh localhost:27000

You should discover yourself on the MongoDB console, where you can begin developing your databases. You can leave the console with the exit command, and then leave the container likewise with the exit command. You can then go back to the MongoDB console with the previous commands whenever it’s time to work with the database again.

For more tutorials from Jack Wallen, register for TechRepublic’s YouTube channel How To Make Tech Work— and remember to like this video.



Source

Leave a Reply

Your email address will not be published. Required fields are marked *