Back to Engineering Notes
DockerEngineering Note

10. Create a Docker Network

Create a custom Docker network named media on App Server 3 in Stratos DC with:

🎯 Objective

Create a custom Docker network named media on App Server 3 in Stratos DC with:

plain text
Network Name → media
Driver       → bridge
Subnet       → 10.10.1.0/24
IP Range     → 10.10.1.0/24

The main command is:

bash
docker network create \
  --driver bridge \
  --subnet 10.10.1.0/24 \
  --ip-range 10.10.1.0/24 \
  media

🧠 Concept

A Docker network allows containers to communicate with each other and with external networks.

Docker provides several network drivers, including:

plain text
bridge
host
none
overlay
macvlan
ipvlan

For this task, we use:

plain text
bridge

A custom bridge network provides an isolated network for containers running on the same Docker host.

plain text
Docker Host
     │
     ▼
┌─────────────────────────────┐
│ media                       │
│ Driver: bridge              │
│ Subnet: 10.10.1.0/24        │
│                             │
│  Container A                │
│       ↕                     │
│  Container B                │
└─────────────────────────────┘

🌉 What Is a Bridge Network?

The bridge driver creates a private network inside the Docker host.

Containers connected to the same custom bridge network can communicate with each other.

For example:

plain text
Docker Host
                  │
        ┌─────────┴─────────┐
        │                   │
        │   media network   │
        │   10.10.1.0/24    │
        │                   │
        │   ┌───────────┐   │
        │   │Container A│   │
        │   └─────┬─────┘   │
        │         ↕           │
        │   ┌───────────┐   │
        │   │Container B│   │
        │   └───────────┘   │
        │                   │
        └───────────────────┘

Custom bridge networks also provide useful features such as container-name DNS resolution between containers on the same network.


🔍 Step 1: Check Existing Docker Networks

List the existing Docker networks:

bash
docker network ls

Typical output:

plain text
NETWORK ID     NAME      DRIVER    SCOPE
xxxxxxxxxxxx   bridge    bridge    local
xxxxxxxxxxxx   host      host      local
xxxxxxxxxxxx   none      null      local

Docker normally creates these default networks automatically:

plain text
bridge
host
none

🚀 Step 2: Create the media Network

Create the required network:

bash
docker network create \
  --driver bridge \
  --subnet 10.10.1.0/24 \
  --ip-range 10.10.1.0/24 \
  media

If Docker requires elevated privileges:

bash
sudo docker network create \
  --driver bridge \
  --subnet 10.10.1.0/24 \
  --ip-range 10.10.1.0/24 \
  media

🧩 Command Breakdown

Network Create

bash
docker network create

Creates a new Docker network.


Driver

bash
--driver bridge

Specifies that the network should use the:

plain text
bridge

driver.

Short form:

bash
-d bridge

Subnet

bash
--subnet 10.10.1.0/24

Defines the network's address space.

The CIDR:

plain text
10.10.1.0/24

represents the subnet used by the Docker network.

Conceptually:

plain text
media
  │
  └── Subnet
       └── 10.10.1.0/24

Docker allocates container addresses from the configured network according to its IPAM configuration.


IP Range

bash
--ip-range 10.10.1.0/24

Specifies the range Docker can use when dynamically assigning IP addresses to containers.

For this task:

plain text
Subnet   → 10.10.1.0/24
IP Range → 10.10.1.0/24

The IP range covers the same CIDR block as the configured subnet.


Network Name

The final argument:

bash
media

sets the network name to:

plain text
media

🔍 Step 3: Verify the Network

List Docker networks again:

bash
docker network ls

You should now see:

plain text
NETWORK ID     NAME      DRIVER    SCOPE
xxxxxxxxxxxx   bridge    bridge    local
xxxxxxxxxxxx   host      host      local
xxxxxxxxxxxx   media     bridge    local
xxxxxxxxxxxx   none      null      local

The important entry is:

plain text
media     bridge

🔬 Step 4: Inspect the Network

For detailed information, run:

bash
docker network inspect media

Look for the IPAM configuration.

You should see information equivalent to:

json
"IPAM": {
    "Config": [
        {
            "Subnet": "10.10.1.0/24",
            "IPRange": "10.10.1.0/24"
        }
    ]
}

This verifies:

plain text
Name     → media       ✅
Driver   → bridge      ✅
Subnet   → 10.10.1.0/24 ✅
IP Range → 10.10.1.0/24 ✅

🧠 Docker IPAM

Docker uses IP Address Management (IPAM) to manage addresses assigned to containers.

For this network:

plain text
media
 │
 ▼
Docker IPAM
 │
 ├── Subnet
 │     └── 10.10.1.0/24
 │
 └── IP Range
       └── 10.10.1.0/24

When containers join the network, Docker can automatically allocate addresses from the configured range.


🐳 Optional: Connect a Container

A container can be created directly on the network:

bash
docker run -d \
  --name app1 \
  --network media \
  nginx

The architecture becomes:

plain text
Docker Host
    │
    ▼
media
10.10.1.0/24
    │
    ▼
app1

Inspect the container:

bash
docker inspect app1

Or inspect the network again:

bash
docker network inspect media

The connected container should appear under:

plain text
Containers

🔗 Optional: Connect Existing Container

An already-running container can also be attached to the network:

bash
docker network connect media <container_name>

For example:

bash
docker network connect media app1

To disconnect it:

bash
docker network disconnect media app1

🗑️ Optional: Remove the Network

If the network is no longer required:

bash
docker network rm media

Docker normally requires containers to be disconnected from the network before the network can be removed.


⚠️ Important Notes

docker network create creates a custom Docker network.
-driver bridge selects the bridge network driver.
-subnet defines the network's address space.
-ip-range defines the address range available for dynamic container allocation.
Custom bridge networks allow containers on the same network to communicate.
Containers on a user-defined bridge can resolve each other by container name.
docker network inspect provides detailed network configuration.
The selected subnet should not conflict with other networks on the Docker host.

⚙️ Complete Workflow

Check existing networks:

bash
docker network ls

Create the network:

bash
docker network create \
  --driver bridge \
  --subnet 10.10.1.0/24 \
  --ip-range 10.10.1.0/24 \
  media

Verify:

bash
docker network ls

Inspect:

bash
docker network inspect media

🧪 Validation Checklist

[ ] Connected to App Server 3
[ ] Docker is running
[ ] Existing networks checked
[ ] Network named media created
[ ] Network uses bridge driver
[ ] Subnet is 10.10.1.0/24
[ ] IP range is 10.10.1.0/24
[ ] Network visible in docker network ls
[ ] Configuration verified using docker network inspect media

📌 Summary

The complete command is:

bash
docker network create \
  --driver bridge \
  --subnet 10.10.1.0/24 \
  --ip-range 10.10.1.0/24 \
  media

Verify it:

bash
docker network inspect media

The final configuration is:

plain text
Docker Host — App Server 3
        │
        ▼
┌────────────────────────────┐
│ Network: media             │
│                            │
│ Driver   → bridge          │
│ Subnet   → 10.10.1.0/24    │
│ IP Range → 10.10.1.0/24    │
│                            │
│      Containers            │
│       ↕    ↕               │
│       ↔    ↔               │
└────────────────────────────┘

The key takeaway is:

> A custom Docker bridge network provides an isolated network where containers can communicate with each other while Docker IPAM manages container addresses within the configured subnet and IP range.