Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
User defined bridge on Docker network
A user-defined bridge network in Docker provides enhanced networking capabilities compared to the default bridge network. It allows containers to communicate using container names instead of IP addresses, offers better isolation, and provides more flexible network management options.
Advantages of User-Defined Bridge Networks
User-defined bridge networks offer several key benefits over default bridge networks:
Name-based communication − Containers can access each other using names or aliases instead of IP addresses
Better isolation − Only containers within the same user-defined network can communicate with each other
Dynamic connectivity − Containers can be connected or disconnected from networks without stopping them
Automatic port exposure − All ports are exposed between containers in the network but not to the external world
Configurable network settings − Custom subnet ranges, gateway addresses, and DNS settings
Creating and Using User-Defined Bridge Networks
Step 1: Create a Bridge Network
Create a user-defined bridge network using the following command:
sudo docker network create --driver bridge my-alpine-net
This command returns the network ID of the newly created network.
Step 2: List Networks
Verify that the network has been created successfully:
sudo docker network ls
You will see the my-alpine-net network listed with the bridge driver and local scope.
Step 3: Inspect the Network
Examine the network configuration:
sudo docker network inspect my-alpine-net
Initially, the network will show no containers associated with it.
Step 4: Create Test Containers
Create three Alpine containers with different network configurations:
# Container connected to default bridge network sudo docker run -dit --name my-alpine-01 alpine ash # Container connected to user-defined network sudo docker run -dit --name my-alpine-02 --network my-alpine-net alpine ash # Container connected to both networks sudo docker run -dit --name my-alpine-03 alpine ash sudo docker network connect my-alpine-net my-alpine-03
Step 5: Inspect Network Associations
Check which containers are connected to each network:
sudo docker network inspect my-alpine-net sudo docker network inspect bridge
Communication Examples
User-Defined Network Communication
Containers in the user-defined network can communicate using names:
# From my-alpine-02 container sudo docker attach my-alpine-02 ping -c 3 my-alpine-03
This command succeeds because both containers are in the my-alpine-net network.
Default Bridge Network Communication
Containers in the default bridge network require IP addresses:
# From my-alpine-03 container sudo docker attach my-alpine-03 ping -c 3 172.17.0.2
This works using the IP address of my-alpine-01, but name-based communication fails:
# This will fail sudo docker attach my-alpine-01 ping -c 3 my-alpine-03
Network Isolation
Comparison
| Feature | Default Bridge | User-Defined Bridge |
|---|---|---|
| Communication Method | IP addresses only | Container names/aliases |
| Network Isolation | All containers can communicate | Only containers in same network |
| Dynamic Connectivity | Requires container restart | Connect/disconnect on-the-fly |
| Port Exposure | Manual configuration needed | Automatic within network |
| DNS Resolution | Not available | Built-in DNS server |
Conclusion
User-defined bridge networks provide superior networking capabilities compared to default bridge networks. They enable name-based communication, better isolation, and dynamic network management, making them the preferred choice for multi-container applications requiring reliable inter-container communication.
