Introduction
For applications to be truly useful, they need to communicate. In Docker, this means understanding how containers connect to each other and to the outside world. This lesson will explain Docker's networking model, focusing on the default bridge network, user-defined bridge networks, and key concepts like DNS resolution within Docker environments. Mastering networking is critical for building multi-service applications.
Key Concepts
Default Bridge Network
When Docker is installed, a default bridge network named bridge is created. All containers that you run without specifying a network are automatically attached to this bridge network. Containers on the default bridge network can communicate with each other via their IP addresses, but not by container name.
-
Isolation: Provides network isolation for containers.
-
IP Addressing: Containers receive an internal IP address from the
bridgenetwork's subnet. -
Limited DNS: Containers can only resolve other containers on the same default bridge by IP address, not by hostname.
User-Defined Bridge Networks
For more robust and flexible networking, Docker allows you to create your own user-defined bridge networks. These networks offer significant advantages over the default bridge network:
-
Automatic DNS Resolution: Containers on a user-defined network can discover and communicate with each other using their container names as hostnames.
-
Better Isolation: Provides explicit isolation for different application stacks.
-
Configurable: Allows for custom subnets, gateways, and other network options.
Network Drivers
Docker supports several network drivers, each suited for different use cases:
-
bridge: The default network driver. Suitable for single-host applications. -
host: Removes network isolation between the container and the Docker host. The container directly uses the host's network stack. -
null: Disables networking for the container. -
overlay: Used for multi-host container communication (e.g., in Docker Swarm). -
macvlan: Assigns a MAC address to a container, making it appear as a physical device on your network.
Example/Code
Inspecting Docker Networks
List all Docker networks:
bashdocker network ls
Inspect a specific network (e.g., the default bridge):
bashdocker network inspect bridge
Creating and Using a User-Defined Network
- Create a new bridge network:
bash
undefined
docker network create my-app-network ```
- Run containers on the new network: Now, launch two Nginx containers, ensuring they are connected to
my-app-network.bashundefined
docker run -d --name web1 --network my-app-network nginx docker run -d --name web2 --network my-app-network nginx ```
- Verify communication: You can now access
web2fromweb1using its name.
For example, from web1's shell:
bashdocker exec -it web1 bash # Inside web1 container capt-get update && apt-get install -y curl curl http://web2 # This should return Nginx's welcome page exit ``` ## Summary/Key Takeaways - Docker uses networks to enable communication between containers and the host. - The default `bridge` network connects containers but lacks name resolution. - User-defined bridge networks (`docker network create`) provide automatic DNS resolution and better isolation. - Different network drivers (`bridge`, `host`, `overlay`, etc.) cater to various architectural needs.