Connecting containers to multiple networks in Docker - Time & Space Complexity
We want to understand how the time to connect a container to multiple networks changes as the number of networks grows.
How does adding more networks affect the work Docker does?
Analyze the time complexity of the following Docker commands.
# Create two networks
docker network create net1
docker network create net2
# Run a container attached to the first network
docker run -d --name mycontainer --network net1 nginx
# Connect the container to the second network
docker network connect net2 mycontainer
This code creates two networks, runs a container on the first, then connects it to the second network.
Look for repeated steps that take time as input grows.
- Primary operation: Connecting the container to each network.
- How many times: Once per network added after the first.
Each new network requires a separate connect operation.
| Input Size (n networks) | Approx. Operations (connect calls) |
|---|---|
| 2 | 1 |
| 10 | 9 |
| 100 | 99 |
Pattern observation: The number of connect operations grows linearly with the number of networks minus one.
Time Complexity: O(n)
This means the time to connect a container to multiple networks grows directly in proportion to how many networks you add.
[X] Wrong: "Connecting a container to multiple networks happens all at once, so time stays the same no matter how many networks."
[OK] Correct: Each network connect is a separate action Docker performs, so more networks mean more work and more time.
Understanding how operations scale with input size helps you explain system behavior clearly and shows you think about efficiency in real tasks.
What if we connected multiple containers to the same set of networks? How would the time complexity change?