Hi Guys! Hope you are doing well. Let’s Learn about “How to Install MongoDB on Docker Container Linux”.
The Docker is an open source platform, where developers can package there application and run that application into the Docker Container.
So It is 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 some configuration files. It was first started in 2013 and developed by Docker, Inc.
Let’s see what we require to Install MongoDB on Docker Container.
Requirement to Install MongoDB on Docker Container.
- We have docker installed on our System, here we are using Ubuntu 20, If you want to Install Docker on Ubuntu 20 then follow the Link.
- Internet Connection to pull the MongoDB images from docker hub.
So, first we will check the docker hub for the MongoDB images or we can pull directly MongoDB images with the help of docker command.
Pull the MongoDB Images for Docker Container
So let,s pull the Image for MongoDB from the docker hub or if we want to pull the MongoDB image for the specific version then use : with image.
$ docker pull mongo
And for the specific version use the below command to pull the MongoDB image.
$docker pull mongo:5.0.2
So this will pull the image of MongoDB 5.0.2 version, you can check the pulled images by using docker images command.
$ docker images
Install MongoDB and Run Docker Container
So we have pulled the Images we have to run the MongoDB container using docker run command.
$ docker run -d mongo
So we have run the docker container for MongoDB, we can check using docker ps command.
$ docker ps
But this will be not accessible on our local system. So we will use many options and run the docker container again.
$ 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 lets understand the parameter we have used to install and run MongoDB on Docker Container.
-d: This will detached mode, MongoDB Container will run on background.
-p: It is to define the port number for the docker container inside the container the port is 27017 and outside the container the port is 27017.
-e: It is used set the environment variables like username and password for MongoDB install docker container.
–name: It is the tag that we have to set for the MongoDB container and we have given the name as “mongodb-tastethelinux-server”.
mongo:5.0.2: It is the image of MongoDB with version 5.0.2, that we have install and run the MongoDB docker container.
Connect MongoDB Container via MongoDB Compass
So, lets open the MongoDB compass Application, it will look like the below image.
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 been on the MongoDB docker container.
Connect MongoDB Container via Shell.
So we have install and run the MongoDB docker container on Ubuntu 20.04, now let’s connect the container via shell.
$ docker exec -it mongodb-tastethelinux-Server /bin/bash
So we are into the MongoDB docker shell, and we can find the difference between my shell vs the docker shell.
Lets try mongo command and check the database that are in the MongoDB docker container.
So we have connected the install MongoDB Docker container by using mongo command.
root@82874c86425a:/# mongo -uadmin -padmin
> show dbs
So now we will create a database into install MongoDB Docker container via shell.
> use tastethelinux
We have switched to database, now lets create the collection with the name “MongoDocker”
> show collections;
So we can see that we have created a database and a collection into the MongoDB Docker container, lets check the same with the MongoDB Compass.
You have to just connect the compass or refresh the screen if you are already logged in.
So we have refresh the application and we can se the new database “tastethelinux” Click on that tastethelinux database, you will found the collection.
So we have successfully Install MongoDB on Docker Container, Create a Database and Collections, Connect with Compass and shell.
Thanks for your time. Keep Supporting US. If you want to install MongoDB on EC2 instances follow the link, Install Docker on CentOS 8, Want to Install Docker CE on Rocky linux 8, Install Docker Compose on Rocky Linux 8, Install Docker on CentOS 7, Install Docker on Ubuntu 20. Thanks Once again to Read the article.