Diving Deeper into Docker: Understanding Networks

Diving Deeper into Docker: Understanding Networks

Have you ever wondered how multiple Docker containers communicate with each other? Docker networking plays a crucial role in enabling containerized applications to work together.

Understanding Docker Networks is crucial for building scalable and efficient systems.

What is Docker Networking?

Docker networking is like a communication system for containers. Just as computers need a network to talk to each other, containers also need networks to communicate effectively.

Why is Networking Needed?

Without a Network, each container behaves like it’s in its own locked room That is its isolated environment, meaning they can’t communicate with other containers. If you’re connecting a frontend container with the backend, then the frontend has no idea where the backend container exists as Docker’s design emphasizes container isolation for security.

Whether you're connecting a frontend container to a backend API or a backend API to a database container, Docker networking is crucial in enabling seamless communication between containers.

Let’s look at how it works🚀

Step-by-Step Example:

Connecting Node.js API and MongoDB Containers

1️⃣ Create a Custom Docker Network

Create a custom bridge network:

docker network create custom-network

custom-network is a name given to the network.

2️⃣ Run the MongoDB Container

Start a MongoDB container and attach it to the custom network:

docker run -d --name mongo-container -v data-vol:/data/db --network custom-network mongo

3️⃣ Set Up Node.js API

Here is a code of Node.js API that connects to MongoDB.

index.js:

const express = require("express");
const mongoose = require("mongoose");
const app = express();

mongoose.connect("mongodb://mongo-container:27017/mydb",{
    useNewUrlParser: true,
    useUnifiedTopology: true,
}).then(()=>{
    console.log("MongoDb connected successfully!!");
}).catch((err)=>{
    console.log("There is an error while connecting to mongodb");
})


app.get("/",async(req,res,next)=>{
    res.status(200).json({
        msg:"api running!!"
    })
})

app.listen(3000,()=>{
    console.log("Server running on port 3000");
})

package.json

{
  "name": "node-api",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "express": "^4.18.2",
    "mongoose": "^7.0.1"
  }
}

4️⃣ Create a Dockerfile for Node.js API

Dockerfile:

FROM node:18-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "index.js"]

5️⃣ Build and Run the Node.js Container

Build the Docker image for Node.js API:

docker build -t node-api .

Run the container and attach it to the same custom network:

docker run -d --name node-api-container --network custom-network -p 3000:3000 node-api

6️⃣ Test the Application

To test the application, open your browser and visit http://localhost:3000.

You should see {"msg":"api running!!"}

7️⃣ Check the logs

You can also check the logs:

docker logs node-api-container

Here’s how it works

Outro

That’s it, everyone!

We’ve explored how Docker's networking mechanism creates seamless communication bridges between multiple containers. I hope this article helped you understand Docker networks more clearly and effectively.

Thank you for reading! 🙌