networking
Docker provides various networking options to facilitate communication between containers and the outside world. Here are some key aspects of Docker networking:
1. Default Network:
By default, Docker provides a bridge network named bridge
. Containers attached to this network can communicate with each other using container names as hostnames.
2. Creating Custom Networks:
You can create custom networks to facilitate communication between specific containers. For example:
3. Connecting Containers to a Network:
When you run a container, you can specify the network it should be attached to:
Containers on the same network can communicate using their container names as hostnames.
4. Inspecting Networks:
To inspect the details of a network, you can use the following command:
This command provides information about the network, including the connected containers.
5. Using Host Network:
You can run a container in the host network namespace, bypassing Docker's network isolation:
This allows the container to use the host network directly.
6. Linking Containers:
Although linking containers is an older approach, it's still available. When one container is linked to another, it can reference the linked container by name:
In this example, container2
can communicate with container1
using the alias alias
.
7. Overlay Networks:
Overlay networks are used for communication between containers running on different Docker hosts in a swarm. They enable multi-host networking:
8. Macvlan Networks:
Macvlan networks allow containers to have their own MAC address, making them appear as physical devices on the network:
This creates a Macvlan network that can be used by containers.
9. User-Defined Bridge Networks:
You can create user-defined bridge networks to isolate containers on the same Docker host:
10. Network Modes:
Docker containers can run in different network modes:
Bridge (default): Isolates containers on the same host.
Host: Uses the host network namespace.
None: No networking for the container.
Container: Shares the network namespace with another container.
11. Port Mapping:
You can map container ports to host ports when running a container:
This maps port 80 in the container to port 8080 on the host.
12. DNS Resolution:
Docker automatically sets up DNS resolution between containers on the same network. Containers can resolve each other by their container names.
These are some basic concepts and commands related to Docker networking. The choice of network type depends on the use case and requirements of your application. Docker provides a versatile set of networking options to cater to different scenarios, from simple isolated networks to complex multi-host setups in swarm mode.