LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
Docker

Creating Custom Bridge Networks

When you start building real-world applications, a single container is rarely enough. A modern architecture usually involves multiple services—for example, a Node.js API communicating with a MongoDB database. For these microservices to talk to each other securely and reliably, they need to be on the same network.

Creating Custom Bridge Networks in Docker

When you start building real-world applications, a single container is rarely enough. A modern architecture usually involves multiple services—for example, a Node.js API communicating with a MongoDB database. For these microservices to talk to each other securely and reliably, they need to be on the same network.

By default, Docker places all new containers on a generic network called the default bridge. However, this default network lacks a critical feature: automatic DNS resolution. This means containers cannot find each other using their names. In this tutorial, we will learn how to create user-defined custom bridge networks to solve this exact problem.

What you will learn
  • Why the default Docker bridge network is not suitable for multi-container applications.
  • How to create a custom, user-defined bridge network using the Docker CLI.
  • How to launch a Node.js API and a MongoDB database on the same custom network.
  • How Docker's embedded DNS server enables containers to securely communicate by their container names.

1. The Problem with the Default Bridge Network

When you install Docker, it automatically creates a network named bridge. If you run a container without specifying a network, Docker attaches it here. While this is fine for quick tests, it becomes a nightmare for production systems.

On the default bridge, containers can only communicate if you know their exact, dynamically assigned IP addresses. Since container IPs change every time they restart, hardcoding an IP like 172.17.0.3 in your Node.js application to connect to MongoDB is extremely fragile.

Feature Default Bridge Network Custom User-Defined Bridge
DNS Resolution Not supported (Requires IP addresses or legacy --link) Automatic (Containers ping each other by name)
Isolation Poor (All unassigned containers share this network) Excellent (Only explicitly attached containers can communicate)
Link Management Requires restarting containers to update links Containers can connect/disconnect on the fly

2. Practical Implementation: Connecting Node.js to MongoDB

To fix the IP address problem, we will create our own network. When containers are placed on a custom network, Docker provides a built-in DNS server. This allows your Node.js app to connect to the database simply by using the database's container name as the host.

Step 1: Create the custom network

Example 1: Creating the network

# Create a new bridge network named 'app-network'
docker network create my-app-network

# Verify it was created successfully
docker network ls

Expected output

NETWORK ID     NAME             DRIVER    SCOPE
1a2b3c4d5e6f   bridge           bridge    local
9x8y7z6w5v4u   my-app-network   bridge    local
...

Step 2: Run the Database on the Network

Now, let's start a MongoDB container and attach it to our new network. We will name the container mongodb.

Example 2: Launching MongoDB

docker run -d \
  --name mongodb \
  --network my-app-network \
  -e MONGO_INITDB_ROOT_USERNAME=admin \
  -e MONGO_INITDB_ROOT_PASSWORD=secret \
  mongo:latest

Step 3: Run the Node.js API on the same Network

Finally, we run our Node.js application. Notice the environment variable DB_HOST. Instead of passing a fragile IP address, we simply pass the container name: mongodb.

Example 3: Launching the Node.js API

docker run -d \
  --name node-api \
  --network my-app-network \
  -p 3000:3000 \
  -e DB_HOST=mongodb \
  -e DB_USER=admin \
  -e DB_PASS=secret \
  my-node-app:latest
Important: Because both containers share my-app-network, the Node.js application can seamlessly connect using the connection string mongodb://admin:secret@mongodb:27017. Docker handles the translation of the word "mongodb" into the correct IP address automatically.

3. Architecture / Workflow Visualized

Let's look at how Docker routes the traffic between these isolated environments under the hood.

Docker Embedded DNS Resolution

Custom Bridge Network (my-app-network) Node.js API Container: node-api Docker DNS 127.0.0.11 MongoDB Container: mongodb 1. "Where is mongodb?" 2. "IP: 172.18.0.3" 3. Direct Secure Traffic over Bridge Network

The Node.js container queries the internal Docker DNS. Once it receives the IP, traffic flows securely between the containers without leaving the isolated bridge network.

4. Step-by-Step Execution Breakdown

When you start these containers on a custom network, a precise sequence of events ensures they can communicate securely.

Step 1: Network Creation: Docker creates a new virtual bridge interface on your host machine. This acts like an isolated virtual switch.
Step 2: Container Attachment: When you run the MongoDB and Node.js containers with --network, Docker attaches virtual ethernet interfaces (veth pairs) to this switch.
Step 3: DNS Registration: The Docker daemon automatically registers the container names (mongodb and node-api) and their newly assigned IP addresses in its internal DNS resolver, located at 127.0.0.11 inside every container.
Step 4: Resolution & Connection: The Node.js app attempts to connect to mongodb:27017. The request hits the internal DNS, resolves to the correct IP, and the connection is established securely.
Watch out: Never use the legacy --link flag. It is deprecated, brittle, and only exists for backward compatibility with the default bridge network. Always create a custom network instead.

5. Hands-On Practice

It's time to solidify this knowledge. Open your terminal and try these exercises.

Beginner Exercise 1: Inspect a Network

Create a custom network and use the inspect command to see its internal details, including the subnet it uses.

Hint: Use docker network create test-net followed by docker network inspect test-net.

Intermediate Exercise 2: Prove DNS Resolution Works

Run two lightweight alpine Linux containers on your custom network. Attach to one of them and try to ping the other by its container name.

Expected result: The ping command should successfully reach the other container and display its resolved IP address.

Advanced Exercise 3: Connect a Running Container

Start a container on the default bridge (do not use the --network flag). Then, without stopping the container, dynamically attach it to your custom network.

Hint: Look into the docker network connect command.

6. Interview Questions

If you are applying for a backend, DevOps, or Full-Stack role, understanding container networking is highly valued. Here is what interviewers look for.

Q1. What is the primary difference between the default bridge network and a user-defined bridge network in Docker?

Answer: The most significant difference is that user-defined bridge networks provide automatic DNS resolution between containers. Containers on a user-defined network can resolve each other by container name or alias. On the default bridge, you must rely on fragile IP addresses or the deprecated --link flag. Additionally, user-defined networks provide better isolation because only explicitly assigned containers join them.

Q2. How exactly do containers on a custom network resolve names to IP addresses?

Answer: When a custom network is created, Docker runs an embedded DNS server inside the network namespace of the containers, accessible at the IP address 127.0.0.11. When an application (like Node.js) makes a request to a hostname (like "mongodb"), the request is routed to this embedded DNS server, which maintains a dynamic map of container names to their current IP addresses.

Conclusion

Mastering Docker networking elevates you from someone who just runs containers to an architect who designs secure, resilient microservices. By utilizing custom bridge networks, you ensure your database remains isolated from the public internet while securely exposing it to your API via automatic DNS resolution. This entirely eliminates IP hardcoding and paves the way for seamless deployments in production.

Official Documentation

Upgradix by Shrash Studio – Learn Technology. Build Your Future.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.