0
0
Dockerdevops~5 mins

Why custom networks matter in Docker - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why custom networks matter
O(n)
Understanding Time Complexity

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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As you add more containers, the total time to start and connect them grows.

Input Size (n)Approx. Operations
1010 container starts and network attachments
100100 container starts and network attachments
10001000 container starts and network attachments

Pattern observation: The time grows directly with the number of containers added.

Final Time Complexity

Time Complexity: O(n)

This means the time to connect containers grows in a straight line as you add more containers.

Common Mistake

[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.

Interview Connect

Understanding how container networking scales helps you design systems that stay fast and reliable as they grow.

Self-Check

What if we used the default bridge network instead of a custom network? How would the time complexity change?