Now let's run some tests to see the practical differences between bridge and host networking in Docker.
First, let's create a container with bridge networking and another with host networking, then compare their performance.
- Let's stop and remove the previous nginx container:
docker stop nginx-host
docker rm nginx-host
- Create a new container with bridge networking to test performance:
docker run --name bridge-test -d --network bridge nginx:alpine
- Create another container with host networking:
docker run --name host-test -d --network host nginx:alpine
- Let's use
docker exec
to run a simple network test in each container:
For the bridge network container:
docker exec bridge-test sh -c "time wget -q -O /dev/null http://google.com"
For the host network container:
docker exec host-test sh -c "time wget -q -O /dev/null http://google.com"
Compare the execution times. Typically, the host network container will have slightly better performance because it avoids the additional network layer.
Examining Network Interfaces
Let's examine the network interfaces in both containers:
- For the bridge network container:
docker exec bridge-test ip addr show
You'll see that this container has its own network interfaces, isolated from the host.
- For the host network container:
docker exec host-test ip addr show
You'll notice that this container has the exact same network interfaces as the host system, including all physical network interfaces.
- Compare with the host's network interfaces:
ip addr show
The host network container's interfaces should match those of the host system.
Understanding Port Conflicts
When using host networking, port conflicts can occur if the container tries to use ports already in use on the host:
- Stop and remove all running containers:
docker stop bridge-test host-test
docker rm bridge-test host-test
- Start a service on the host using port 8080:
python3 -m http.server 8080 &
- Now try to run a container with host networking that also wants to use port 8080:
docker run --name conflict-test --network host -d -p 8080:80 nginx:alpine
You should see an error because port 8080 is already in use on the host.
- Clean up the Python HTTP server:
kill %1
This demonstrates one potential drawback of host networking - you need to be aware of port conflicts with the host.