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?"
Practice
Solution
Step 1: Understand network isolation concept
Network isolation means keeping services separate so they don't communicate unless allowed.Step 2: Identify the main goal
The main goal is to control communication and improve security between services.Final Answer:
To keep services separate and control their communication -> Option AQuick Check:
Network isolation = separate and control communication [OK]
- Confusing network isolation with image size optimization
- Thinking it speeds up container startup
- Assuming it updates containers automatically
Solution
Step 1: Identify command to create network
The command to create a Docker network isdocker network createfollowed by the network name.Step 2: Match correct syntax
docker network create my-network uses the correct syntax:docker network create my-network.Final Answer:
docker network create my-network -> Option CQuick Check:
Creating network = docker network create [OK]
- Using 'docker container run' to create networks
- Confusing image build with network creation
- Trying to start a service to create a network
Solution
Step 1: Understand Docker network isolation
Containers on different custom networks cannot communicate or resolve each other's names by default.Step 2: Analyze ping behavior
Since they are on separate networks, ping by container name will fail due to no DNS resolution across networks.Final Answer:
Ping fails because containers on different networks cannot resolve each other's names -> Option BQuick Check:
Different networks = no name resolution [OK]
- Assuming default bridge allows all communication
- Thinking image similarity affects network reachability
- Believing Docker allows cross-network DNS by default
Solution
Step 1: Check network attachment
If services are on the same network, they should communicate unless other factors block it.Step 2: Consider Docker host location
If containers run on different Docker hosts without overlay network, they cannot communicate even on the same network name.Final Answer:
The containers are running on different Docker hosts -> Option DQuick Check:
Different hosts without overlay = no communication [OK]
- Assuming same network name means same network across hosts
- Ignoring network driver type
- Confusing container names with network connectivity
Solution
Step 1: Understand isolation needs
To isolate two services from others, they must be on a private network only they share.Step 2: Choose network setup
Creating a custom network and attaching only these two services ensures isolation and communication.Step 3: Evaluate other options
Default bridge includes all containers, multiple networks reduce isolation, host network removes isolation.Final Answer:
Create a custom network and attach only these two services to it -> Option AQuick Check:
Custom network with only two services = isolation + communication [OK]
- Using default network which includes all containers
- Attaching services to multiple networks losing isolation
- Using host network which disables isolation
Ready to prove it?
Pass the test and this pattern moves unknown
