How to Create Custom Docker Network Easily
Use the
docker network create command followed by a network name and optional driver type to create a custom Docker network. For example, docker network create my-network creates a new bridge network named my-network.Syntax
The basic syntax to create a custom Docker network is:
docker network create [OPTIONS] NETWORK_NAME
Here, NETWORK_NAME is the name you want to give your network.
Common options include:
--driver: Specifies the network driver (default isbridge).--subnet: Defines a custom subnet for the network.--gateway: Sets a gateway IP address.
bash
docker network create [OPTIONS] NETWORK_NAME
Example
This example creates a custom bridge network named my-custom-network. Then it lists all Docker networks to show the new network.
bash
docker network create my-custom-network docker network ls
Output
NETWORK ID NAME DRIVER SCOPE
b1a2c3d4e5f6 bridge bridge local
c7d8e9f0a1b2 host host local
d3e4f5a6b7c8 none null local
f9e8d7c6b5a4 my-custom-network bridge local
Common Pitfalls
Common mistakes when creating custom Docker networks include:
- Not specifying a unique network name, which can cause conflicts.
- Forgetting to specify the
--driveroption when a different driver is needed. - Using invalid subnet or gateway IP addresses.
- Assuming containers automatically connect to the new network without specifying it.
Always check your network list with docker network ls after creation.
bash
docker network create bridge # Wrong: 'bridge' is a default network name and cannot be recreated docker network create --driver overlay my-overlay-network # Right: specify a valid driver and unique name
Quick Reference
| Option | Description | Example |
|---|---|---|
| --driver | Choose network driver (bridge, overlay, macvlan) | --driver bridge |
| --subnet | Set custom subnet in CIDR format | --subnet 192.168.1.0/24 |
| --gateway | Set gateway IP address | --gateway 192.168.1.1 |
| NETWORK_NAME | Name of the custom network | my-network |
Key Takeaways
Use 'docker network create NETWORK_NAME' to make a custom Docker network.
Specify '--driver' to choose the network type; 'bridge' is default for local networks.
Check your networks anytime with 'docker network ls'.
Avoid using default network names like 'bridge' to prevent errors.
Custom networks help isolate and connect containers cleanly.