When creating a Docker network, you can specify a custom subnet and gateway using the docker network create command with the --subnet and --gateway parameters.
🧩 Example Command
docker network create \
--driver=bridge \
--subnet=172.24.0.0/16 \
--gateway=172.24.0.1 \
mail-network
🔍 Explanation
| Parameter | Description |
|---|---|
--driver=bridge | Specifies the network driver. The default is bridge. While optional, it’s good practice to include it explicitly. |
--subnet=172.24.0.0/16 | Defines the IP subnet for the custom Docker network. |
--gateway=172.24.0.1 | Sets the gateway address for the network. |
mail-network | The name of the Docker network being created. |
✅ Verify the Network
After creation, check the network details with:
docker network inspect mail-network
You’ll see output similar to:
"IPAM": {
"Config": [
{
"Subnet": "172.24.0.0/16",
"Gateway": "172.24.0.1"
}
]
}
💡 Notes
- This setup is useful when you need containers to communicate in a predictable private IP range.
- You can attach containers to this custom network using
--network mail-networkwhen running or creating them. - To list all existing Docker networks, use:
docker network ls
In summary:
To create a Docker bridge network with a specific IP range and gateway, usedocker network create --subnet=<CIDR> --gateway=<IP> <network_name>.
This allows precise control over container networking in your environment.