How to Manage MongoDB with Docker containers.

Introduction.

This article is on “How you can Manage MongoDB with Docker Containers.”

Docker is an open-source platform, where developers can package their applications. And run that application into the Docker Container.

Docker is a PAAS (Platform as a Service). Which uses a OS virtualisation to deliver software in packages called containers.

The containers are the bundle of the packages, library files, and configuration files. It was first started in 2013 and developed by Docker, Inc.

So using MongoDB with docker containers enables High Availability and scalability.

In this article, we will look into the following points,

  1. Requirement to Setup the MongoDB Docker Container.
  2. Setting up a MongoDB Container.
  3. Interacting with MongoDB Docker Container.
  4. Interacting with MongoDB Compass to create a first database.

Requirement

  1. We have to setup Docker in our local system.
  2. We should have sudo permission on our system or server to setup docker.

Refer to the links if you want to set up a Docker container in Ubuntu. Setup the Docker container in CentOS. Setup the Docker container in Linux Mint. And Setup the Docker container in Rocky Linux.


How to Setting up the Docker Container for MongoDB.

So let’s go to the Docker Hub and check for the mongo image and pull the image via the docker command.

How to Install MongoDB on Docker Container linux Docker hub
Docker Hub for MongoDB.
How to Install MongoDB on Docker Container linux Docker Pull
docker pull image_name
$ docker pull mongo

Now let’s suppose we want to use a specific version, then use the :(colon) after the image with the required version.

$ docker pull mongo:5.0.2

So this will pull the image of MongoDB 5.0.2 version, now check the pulled images by using the docker images command.

$ docker images
tastetethelinux docker images
docker images

So now run the MongoDB container using the docker run command.

$ docker run -d  \
-p27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=admin \
--name mongodb-tastethelinux-server mongo:5.0.2

So let’s use the docker ps command to check whether the container is running or not.

How to Install MongoDB on Docker Container linux Docker ps
Docker ps
$ docker ps

So let’s understand the parameter we have used to run the MongoDB on Docker Container.

-d: This options is for detached mode, it will run MongoDB Container in background.

-p: This options is for Port Number that have to use in the docker container and outside the docker container. The inside port will be for only within the docker environment. Outside port is for the Public use.

-e: It is to set the environment variables like username and password.

–name: This to set the name of the docker containers. In our example we have set “mongodb-tastethelinux-server”.

mongo:5.0.2: It is the image of MongoDB with version 5.0.2, that we have pulled and run the MongoDB docker container.


How to Interact with MongoDB Docker Container.

Now we will see how to interact with shell using docker. It is same as the ssh that we take on the VM or a server.

Execute the shell on the docker container tastethelinux
Shell for MongoDB docker container
$ docker exec -it mongodb-tastethelinux-Server /bin/bash

The “docker exec” is to execute the command in a container. -it option is to interact with the shell.

Now login via shell and then create the first database with the collection.

Connect the MongoDB Docker Container tastethelinux
Connect MongoDB on Docker container via Shell
root@82874c86425a:/# mongo -uadmin -padmin
> show dbs
MongoDB Docker Shell
> use tastethelinux

We have switched to database, now lets create the collection with the name “MongoDocker”

> db.createCollection('MongoDocker');
> show collections;

So collections are basically the tables in the MongoDB. You can also create collection by inserting your first data.


How to Interact with MongoDB Compass to create a first database.

Developers, DBA use the MongoDB compass to manage there daily activities. We can import and export the database with the help of MongoDB compass.

Lets open the MongoDB compass application and then create the database and collections.

How to Install MongoDB on Docker Container linux Compass connections
Select Fill in Connection fields individually.

So we will click on “Fill in Connection fields individually”, after that we will fill the details, that we have used at the time to run the container.

How to Install MongoDB on Docker Container Filled the connection details.
Connection Details

So lets fill the connection details Hostname will be localhost, port number will be 27017, Authentication mode will be Username/Password.

Username is admin, password is admin, and the authentication database is also an admin, once done just click on connect.

Check the database on Docker Container tastethelinux
MongoDB Database

So we can see the databases that has already in the container. admin, config, and local are the by default databases that are present in MongoDB.

You have to just connect the compass or refresh the screen if you are already logged in.

Refresh the application.

So after the refresh we ware able to see the “tastethelinux” database. Click on that database, you will found the collection named as “MongoDocker”.

Created collection on Docker Container.

You can use compass to create, update, insert or delete queries. Also, you can manage the database with the help of compass.


One Reply to “How to Manage MongoDB with Docker containers.”

Give your valuable time