Why custom networks matter in Docker - Performance Analysis
When using Docker, custom networks help containers talk to each other in a controlled way.
We want to see how the time to connect containers changes as we add more containers to a custom network.
Analyze the time complexity of creating a custom Docker network and connecting multiple containers to it.
# Create a custom network
docker network create my_custom_network
# Run multiple containers attached to this network
for i in $(seq 1 5); do
docker run -d --net my_custom_network --name container_$i alpine sleep 1000
done
This code creates one custom network and then starts several containers connected to it.
Look for repeated actions that affect time.
- Primary operation: Starting each container and attaching it to the custom network.
- How many times: Once per container, repeated for each container started.
As you add more containers, the total time to start and connect them grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 container starts and network attachments |
| 100 | 100 container starts and network attachments |
| 1000 | 1000 container starts and network attachments |
Pattern observation: The time grows directly with the number of containers added.
Time Complexity: O(n)
This means the time to connect containers grows in a straight line as you add more containers.
[X] Wrong: "Adding more containers to a custom network happens instantly, no matter how many containers there are."
[OK] Correct: Each container needs to be started and connected, so the total time adds up with each container.
Understanding how container networking scales helps you design systems that stay fast and reliable as they grow.
What if we used the default bridge network instead of a custom network? How would the time complexity change?