0
0
Dockerdevops~5 mins

Creating custom bridge networks in Docker - Step-by-Step CLI Walkthrough

Choose your learning style9 modes available
Introduction
Sometimes, you want your Docker containers to talk to each other in a private space without interference. Creating a custom bridge network lets you control how containers connect and communicate securely on your machine.
When you want to isolate a group of containers from others on your system for security.
When you need containers to communicate using custom network settings like specific IP ranges.
When you want to give containers easy-to-remember names to connect instead of IP addresses.
When you want to avoid conflicts with Docker's default network settings.
When you want to connect containers across multiple Docker hosts using the same network name.
Commands
This command creates a new custom bridge network named 'my-custom-network' so containers can connect to it and communicate privately.
Terminal
docker network create --driver bridge my-custom-network
Expected OutputExpected
3d2e1f4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3
--driver bridge - Specifies the network type as a bridge network.
Lists all Docker networks so you can verify that 'my-custom-network' was created successfully.
Terminal
docker network ls
Expected OutputExpected
NETWORK ID NAME DRIVER SCOPE 3d2e1f4a5b6c bridge bridge local 7f8g9h0i1j2k host host local 9l0m1n2o3p4q none null local 3d2e1f4a5b6c my-custom-network bridge local
Runs a new container named 'my-app' using the nginx image and connects it to the custom bridge network so it can communicate with other containers on this network.
Terminal
docker run -d --name my-app --network my-custom-network nginx
Expected OutputExpected
a1b2c3d4e5f67890123456789abcdef0123456789abcdef0123456789abcdef0
--network my-custom-network - Connects the container to the custom bridge network.
-d - Runs the container in detached mode (in the background).
Shows detailed information about the custom bridge network, including connected containers and IP address ranges.
Terminal
docker network inspect my-custom-network
Expected OutputExpected
[ { "Name": "my-custom-network", "Id": "3d2e1f4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3", "Created": "2024-06-01T12:00:00.000000000Z", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.18.0.0/16", "Gateway": "172.18.0.1" } ] }, "Containers": { "a1b2c3d4e5f67890123456789abcdef0123456789abcdef0123456789abcdef0": { "Name": "my-app", "EndpointID": "e1f2a3b4c5d6e7f8a9b0c1d2e3f4g5h6i7j8k9l0m1n2o3p4q5r6s7t8u9v0w1x2", "MacAddress": "02:42:ac:12:00:02", "IPv4Address": "172.18.0.2/16", "IPv6Address": "" } }, "Options": {}, "Labels": {} } ]
Key Concept

If you remember nothing else from this pattern, remember: custom bridge networks let your containers talk privately and safely with custom settings.

Common Mistakes
Not specifying the --driver bridge flag when creating the network.
Docker might create a network with a different driver, causing unexpected behavior or incompatibility.
Always include --driver bridge to ensure the network is a bridge type.
Forgetting to connect containers to the custom network using --network flag.
Containers will connect to the default network and won't communicate on the custom network.
Use --network my-custom-network when running containers to connect them properly.
Assuming the network is created without checking with docker network ls.
The network might fail to create due to name conflicts or other errors, causing confusion later.
Always verify network creation with docker network ls before running containers.
Summary
Create a custom bridge network with docker network create --driver bridge my-custom-network.
Verify the network exists using docker network ls.
Run containers connected to this network using docker run --network my-custom-network.
Inspect the network details and connected containers with docker network inspect my-custom-network.