Docker Network
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 Commands
See All Docker Networks
Create a New Network
docker network create mongo-network
Now we can set up both our containers inside this common network so they can interact with each other without any port.
Why Use Docker Networks?
| 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 |
Setting Up MongoDB and Mongo Express Containers
Setting Up MongoDB Container
docker run -d \
-p 27017:27017 \
--name mongo \
--network mongo-network \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=qwerty \
mongo
Since the image is not available locally, Docker will first fetch it.
After downloading all layers, our container is successfully running.
Verify MongoDB Container
This verifies that the mongo container is running.
Setting Up Mongo Express Container
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-express
Important
In 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.
Verify Both Containers
Now we have two running containers.
| Container | Purpose |
|---|
mongo | MongoDB Database |
mongo-express | MongoDB User Interface |
Accessing Mongo Express
Visit:
in your browser.
If a sign-in option appears, Mongo Express is working successfully.
Default Credentials
| 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.
Creating Our Database
Create a new database called:
Inside it, create a collection called:
We can also add a new document:
{
"email": "john@yahoo.in",
"username": "JohnDoe",
"password": "secret"
}
Testing the Connection
Now let's test whether our MongoDB database is connected to our Node.js application.
Send a GET request to:
The 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.
End-to-End Flow
User
|
v
Node.js App (Port 5050)
|
v
MongoDB Container (mongo)
^
|
Mongo Express (Port 8081)
- User sends a request to the Node.js application.
- Node.js communicates with the MongoDB container.
- Data is stored or retrieved from the
college-db database.
- Mongo Express allows us to view and manage the same data through the browser.
Testing with POST Request
We can also test with a POST request from the application's home page.
Steps
- Open the application UI.
- Create a new account.
- Check Mongo Express for the newly added document.
- Send a GET request to:
- Verify that the new user data is returned.
This confirms that:
- The Node.js application is connected successfully.
- MongoDB is running inside a Docker container.
- Mongo Express can manage the database visually.
- All components are communicating correctly through the Docker network.