Before we proceed to MongoDB, there is an important concept we need to learn — Docker Network.
Currently, when we have containers on our host system, each container has its own ports. These containers cannot interact with each other without going through some port.
But when we set up MongoDB and Mongo Express for our Node.js application, we want both their containers to interact with each other without any port limitations.
For this purpose, we set up something called a Docker Network.
Docker has the ability to create isolated networks — if we put two containers inside the same network, they can interact with each other directly without needing any port, localhost, or anything else.
docker network lsdocker network create mongo-networkNow we can set up both our containers inside this common network so they can interact with each other without any port.
| Without Docker Network | With Docker Network |
|---|---|
| Containers communicate through exposed ports | Containers communicate directly |
| Requires port mappings | No port dependency between containers |
| More configuration required | Simpler container-to-container communication |
| Uses localhost and ports | Uses container names as hostnames |
docker run -d \
-p 27017:27017 \
--name mongo \
--network mongo-network \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=qwerty \
mongoSince the image is not available locally, Docker will first fetch it.
After downloading all layers, our container is successfully running.
docker psThis verifies that the mongo container is running.
docker run -d \
-p 8081:8081 \
--name mongo-express \
--network mongo-network \
-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=qwerty \
-e ME_CONFIG_MONGODB_URL=mongodb://admin:qwerty@mongo:27017/ \
mongo-expressIn the ME_CONFIG_MONGODB_URL, the @mongo part refers to the name of the MongoDB container.
mongodb://admin:qwerty@mongo:27017/If you named your MongoDB container something different, use that container name instead.
Since both containers are inside the same Docker network, Mongo Express can communicate directly with MongoDB using the container name.
docker psNow we have two running containers.
| Container | Purpose |
|---|---|
mongo | MongoDB Database |
mongo-express | MongoDB User Interface |
Visit:
http://localhost:8081in your browser.
If a sign-in option appears, Mongo Express is working successfully.
| Field | Value |
|---|---|
| Username | admin |
| Password | pass |
After signing in, you will see the default MongoDB database setup.
Here we can see that:
mongo container → runs the actual MongoDB database.mongo-express container → provides the graphical user interface to view and manage that database.Create a new database called:
college-dbInside it, create a collection called:
usersWe can also add a new document:
{
"email": "john@yahoo.in",
"username": "JohnDoe",
"password": "secret"
}Now let's test whether our MongoDB database is connected to our Node.js application.
Send a GET request to:
localhost:5050/get-usersThe output shows our document.
This means the same document we added through Mongo Express is now visible through our Node.js application.
We did not install MongoDB on our local system, yet our Node.js application was able to connect to the MongoDB database running inside the Docker container.
User
|
v
Node.js App (Port 5050)
|
v
MongoDB Container (mongo)
^
|
Mongo Express (Port 8081)college-db database.We can also test with a POST request from the application's home page.
localhost:5050/get-usersThis confirms that: