0
0
Dockerdevops~5 mins

Network drivers (bridge, host, overlay, none) in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Docker network drivers control how containers communicate with each other and the outside world. They solve the problem of connecting containers in different ways depending on your needs.
When you want containers on the same host to communicate privately using a bridge network.
When you want a container to share the host's network stack directly for better performance.
When you need containers across multiple hosts to communicate securely using an overlay network.
When you want to disable all networking for a container for security or testing.
Commands
This command creates a new bridge network named 'my-bridge-network' for containers to communicate on the same host.
Terminal
docker network create my-bridge-network
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6
Runs a container named 'container1' attached to the 'my-bridge-network' bridge network so it can communicate with other containers on this network.
Terminal
docker run -d --name container1 --network my-bridge-network nginx
Expected OutputExpected
d1e2f3g4h5i6j7k8l9m0n1o2p3q4r5s6t7u8v9w0x1y2z3a4b5c6
--network - Specifies which network the container connects to
Creates a network using the host driver where containers share the host's network stack directly.
Terminal
docker network create --driver host my-host-network
Expected OutputExpected
z9y8x7w6v5u4t3s2r1q0p9o8n7m6l5k4j3i2h1g0f9e8d7c6b5a4
--driver - Sets the network driver to host
Runs a container named 'container2' using the host network driver, so it shares the host's network directly without isolation.
Terminal
docker run -d --name container2 --network host nginx
Expected OutputExpected
f1e2d3c4b5a697887766554433221100ffeeddccbbaa99887766554433221100
--network - Connects the container to the host network
Creates an overlay network named 'my-overlay-network' to allow containers on different Docker hosts to communicate securely.
Terminal
docker network create --driver overlay my-overlay-network
Expected OutputExpected
1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
--driver - Sets the network driver to overlay
Creates a network with no connectivity, isolating containers completely from any network.
Terminal
docker network create --driver none my-none-network
Expected OutputExpected
abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890
--driver - Disables networking for containers on this network
Key Concept

If you remember nothing else from this pattern, remember: Docker network drivers control how containers connect and communicate, choosing the right driver fits your app's communication needs.

Common Mistakes
Not specifying the network driver when creating a network and expecting a specific behavior.
Docker defaults to the bridge driver, which may not work for multi-host communication or host networking needs.
Always specify the correct driver with --driver when creating a network if you need host, overlay, or none behavior.
Running containers without specifying the network and expecting them to communicate across hosts.
Containers on the default bridge network cannot communicate across different Docker hosts.
Use an overlay network for multi-host container communication.
Using the host network driver on Windows or Mac Docker Desktop environments.
Host networking is not supported on Windows or Mac Docker Desktop, causing errors or unexpected behavior.
Use bridge or overlay networks on these platforms instead.
Summary
Create networks with different drivers to control container communication modes.
Use bridge for same-host container communication, host for sharing host network, overlay for multi-host, and none to isolate.
Attach containers to these networks using the --network flag during docker run.