How to Create Network in Docker: Simple Steps and Examples
To create a network in Docker, use the
docker network create command followed by the network name. This command sets up a new isolated network that containers can join for communication.Syntax
The basic syntax to create a Docker network is:
docker network create [OPTIONS] NETWORK_NAME
Here, NETWORK_NAME is the name you want to give your network. [OPTIONS] can include network driver type and other settings.
bash
docker network create [OPTIONS] NETWORK_NAME
Example
This example creates a user-defined bridge network named my_bridge. Containers connected to this network can communicate with each other.
bash
docker network create my_bridge
Output
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6
Common Pitfalls
Common mistakes when creating Docker networks include:
- Using reserved network names like
bridge,host, ornonewhich are default networks. - Not specifying the driver when a specific network type is needed (default is
bridge). - Trying to create a network with a name that already exists.
Example of wrong and right usage:
bash
# Wrong: Using reserved name docker network create bridge # Right: Use a custom name docker network create custom_bridge
Output
Error response from daemon: network with name bridge already exists
<network ID string>
Quick Reference
| Command | Description |
|---|---|
| docker network create NETWORK_NAME | Create a new network with default bridge driver |
| docker network create -d driver_name NETWORK_NAME | Create a network with a specific driver (e.g., overlay, macvlan) |
| docker network ls | List all Docker networks |
| docker network rm NETWORK_NAME | Remove a Docker network |
Key Takeaways
Use
docker network create NETWORK_NAME to make a new Docker network.Avoid reserved names like bridge, host, and none for custom networks.
Specify the driver with
-d option if you need a network type other than bridge.Check existing networks with
docker network ls before creating new ones.Remove unused networks with
docker network rm NETWORK_NAME to keep your environment clean.