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,
- Requirement to Setup the MongoDB Docker Container.
- Setting up a MongoDB Container.
- Interacting with MongoDB Docker Container.
- Interacting with MongoDB Compass to create a first database.
- We have to setup Docker in our local system.
- We should have sudo permission on our system or server to setup docker.
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.
$ 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
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.
$ 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.
$ 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.
[email protected]:/# mongo -uadmin -padmin
> show dbs
> use tastethelinux
We have switched to database, now lets create the collection with the name “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.
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.
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.
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.
So after the refresh we ware able to see the “tastethelinux” database. Click on that database, you will found the collection named as “MongoDocker”.
You can use compass to create, update, insert or delete queries. Also, you can manage the database with the help of compass.