Network isolation between services in Docker - Time & Space Complexity
We want to understand how the time it takes to isolate networks between Docker services changes as we add more services.
How does the work grow when we connect or separate many services?
Analyze the time complexity of the following Docker commands for network isolation.
# Create a custom network
docker network create isolated_net
# Run multiple services attached to the network
for i in $(seq 1 5); do
docker run -d --net isolated_net --name service_$i myservice
done
# Connect an existing container to the network
docker network connect isolated_net service_1
# Disconnect a container from the network
docker network disconnect isolated_net service_2
This code creates a network and runs several services attached to it, then connects and disconnects containers to isolate them.
Look for repeated actions that affect time.
- Primary operation: Loop running containers and attaching them to the network.
- How many times: Once per service, here 5 times but can be more.
As the number of services grows, the number of network attach operations grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 attach operations |
| 100 | 100 attach operations |
| 1000 | 1000 attach operations |
Pattern observation: The work grows directly with the number of services.
Time Complexity: O(n)
This means the time to isolate networks grows in a straight line as you add more services.
[X] Wrong: "Adding more services won't affect the time because networks are created once."
[OK] Correct: Each service must be connected or disconnected individually, so more services mean more work.
Understanding how operations grow with the number of services helps you design scalable Docker setups and explain your reasoning clearly.
"What if we used Docker Compose to start all services at once? How would that change the time complexity?"