Docker Mongodb Tutorial : Use it With Docker and MongoExpress ?

Docker Mongodb Tutorial featured image

Data Scientist Programmer chooses MongoDB as the best choice for storing the data. And with docker, you make opportunities to play with it. As you already know docker plays an important role during the deployment of your project. In this entire docker MongoDB tutorial, you will know how to the following things.

How to Create a Docker Network Bridge?

Connect MongoDB with the host and Network Bridge?

How to bind the MongoDB container with another Container?

See the Database online using MongoExpress.

How to Create a Docker Network Bridge?

By default, Docker uses a bridge network for communicating with the containers. You can also create your own network for the containers. Like in our example We are doing all the things in different network hosts. Let’s create it.

Find all the list of network hosts.

sudo docker network ls

Create a Network host and again type the sudo docker network ls for all lists of the network.

sudo docker network create --driver=bridge my-net
create docker network host
Creation of a new docker network

You can find the gateway and the IP address and driver for the network using the inspect command.

sudo docker network inspect my-net
create docker network host inspecting
Inspect the Docker Network

Connect MongoDB with the host and Network Bridge

Now let’s start a MongoDB container in the above network and communicate directly with the host and container. For this, I will use the docker run command with some commands to run MongoDB in the background. Write the following command in your terminal.

docker run --name datasciencelearner.com-mongo --network my-net -p 27019:27017 -d mongo
running a mongodb server in background
MongoDB Server

And now if you will inspect the my-net network then you will see  datasciencelearner.com-mongo container running in your network bridge. Here the following things had done. The publish port -p 27019:27017 will the map the MongoDB port 27019 of your host with the MongoDB port 27017 of the container.

sudo docker network inspect my-net

 

inspecting the network after running mongodb docker
Inspect the network

You can see MongoDB container with the name datasciencelearner.com-mongo running in the my-net network

How to bind the MongoDB container with another Container?

Now, let’s bind the other container with the MongoDB. In this example, I am using Mongoexpress with the MongoDB container. Run the following command.

sudo docker run --rm --name datasciencelearner-exp --network my-net -e ME_CONFIG_MONGODB_SERVER=datasciencelearner.com-mongo --link datasciencelearner.com-mongo -d -p 8082:8081 mongo-express
running a mongoexpress in background
MongoExpress Command

Here I am adding the environment variable -e ME_CONFIG_MONGODB_SERVER to the name of MongoDB container (datasciencelearner-mongo).Now when you go to your browser and type localhost:8081 then you will see the MongoExpres Interface that is communicating with the MongoDB container. You will see it like this.

checking mongoexpress in browser
checking mongoexpress in browser

After doing all these things if you inspect the docker network my-net you will see the two containers running on it.  One for MongoDB and one for MongoExpress.

sudo docker network inspect my-net
inspecting the network after running mongodb and mongoexpress containers
Inspecting the Network

Doing all the above steps you have successfully deployed MongoDB and Mongoexpress. I hope You have learned how to create a network in docker and bind it with other containers in this Docker MongoDB tutorial. If you are running one or two containers then it is fine to do all the things on the terminal. But I will recommend you to define all these things on the Docker Compose file for full automation.

If you have any query you can message us on Data Science Learner Facebook Page.  You can also contact us for more info.

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner