How to Connect Two Docker Containers Over a Custom Bridge Network (Step-by-Step Guide)

📝 Introduction

Modern applications rarely run as a single service. In containerized environments, networking between services is fundamental.

In this guide, we’ll create two containers:

  • A web server (Nginx)
  • A test client (Alpine Linux)

We’ll connect them using a custom Docker bridge network and verify communication between them.

This is the foundation of microservices networking.


🔹 Prerequisites

  • Linux machine (or VM)
  • Docker installed
  • Basic CLI knowledge

Check Docker:

docker --version

If not installed, follow the official guide from Docker.


Step 1 – Create a Custom Bridge Network

By default, Docker creates a bridge network, but best practice is to create your own isolated network.

docker network create my_custom_network

Verify:

docker network ls

Inspect it:

docker network inspect my_custom_network

You’ll see:

  • Subnet
  • Gateway
  • Driver type (bridge)

Step 2 – Run the First Container (Nginx Web Server)

We’ll use the official image from Docker Hub:

docker run -d \
--name webserver \
--network my_custom_network \
nginx

Check it’s running:

docker ps

Step 3 – Run the Second Container (Alpine Client)

Now launch a lightweight Alpine container:

docker run -it \
--name client \
--network my_custom_network \
alpine sh

Inside the container, install curl:

apk add curl

Step 4 – Test Connectivity Using Container Name

This is the key concept.

Docker provides internal DNS resolution inside user-defined bridge networks.

From inside the Alpine container:

curl http://webserver

If everything is correct, you’ll see the default Nginx welcome page.

Why does this work?

Because Docker automatically registers container names in its internal DNS when using a custom bridge network.


Step 5 – Verify Network Isolation

From your host machine:

curl http://localhost

This will NOT work unless you publish ports.

That’s because we did not expose any ports externally.

To expose Nginx:

docker rm -f webserverdocker run -d \
--name webserver \
--network my_custom_network \
-p 8080:80 \
nginx

Now:

curl http://localhost:8080

🔍 What You Just Learned

  • How Docker bridge networking works
  • Internal DNS resolution between containers
  • Service isolation principles
  • Port publishing to host
  • Basic microservice communication model

🧱 Why This Matters

This exact pattern is used in:

  • Kubernetes pod networking
  • Microservices architectures
  • API-to-database communication
  • Service mesh environments

Understanding this at Docker level makes advanced orchestration tools easier to grasp later.