What is Docker Network?
Container networking refers to the ability for containers to connect to and communicate with each other, or to non-Docker workloads.
Containers
have networking enabled by default, and they can make outgoing connections.
A container
has no information about what kind of network it's attached to, or whether their peers are also Docker workloads or not. A container
only sees a network interface with an IP address, a gateway, a routing table, DNS services, and other networking details. That is, unless the container uses the none
network driver.
Types of Docker Network
There are seven types of Docker Network: -
Default Bridge Network:
The default network driver where containers can only access each other by IP addresses.Custom/User-Defined Bridge Network:
Provide automatic DNS resolution between containers.Host Network:
Remove network isolation between the container and the Docker host.None Network:
Completely isolate a container from the host and other containers.MacvLAN Network:
Assign a MAC address to a container.Overlay Network:
Connect multiple Docker daemons together.IPvLAN Network:
Provide full control over both IPv4 and IPv6 addressing.
When we install Docker
, then bridge
, host
and none
networks get created by default.
Bridge Network
Bridge
network is is the default network. Whenever you start Docker, a bridge network gets created and all newly started containers will connect automatically to the default bridge network.
All containers attached to this network by default and they get an internal IP address. The containers can access each other using this Internal IP, if required.
You can use this whenever you want your containers running in isolation to connect and communicate with each other. Since containers run in isolation, the bridge network solves the port conflict problem. Containers running in the same bridge network can communicate with each other, and Docker uses iptables on the host machine to prevent access outside of the bridge.
docker run -d --name <container_name> <image_Name> -> Default network will get atttached to the container.
docker run -d --name <container_name> -p <hostport>:<containerport> <image_Name> -> To access containers from outside.
docker network create <networkname> -> To create custom network.
docker run -d --name <contaniner_name> --network <networkname> <imagename> -> Custon metwork will get attached to the container.
Host Network
As the name suggests, host
network use the networking provided by the host machine. And it removes network isolation between the container and the host machine where Docker is running.
For example, If you run a container that binds to port 80 and uses host networking, the container’s application is available on port 80 on the host’s IP address. You can use the host network if you don’t want to rely on Docker’s networking but instead rely on the host machine networking.
One limitation with the host
network is that it doesn’t work on Docker desktop: you need a Linux host to use it.
The downside with the host
network is that you can’t run multiple containers on the same host having the same port. Ports are shared by all containers on the host machine network.
docker run -d --name <container_name> --network host <image_name>
None Network
The none
network driver does not attach containers to any network. Containers do not access the external network or communicate with other containers. as they run in an isolated network. You can use it when you want to disable the networking on a container.
docker run -d --name <container_name> --network none <image_name>
Docker Network Commands
List networks
docker network ls
Display detailed information on one or more networks
docker network inspect <networkname>
Connect a container to a network
docker network connect <networkname> <container_name>
Disconnect a container from a network
docker network disconnect <networkname> <container_name>
Removing a network
docker network rm <networkname> docker network prune -> Remove all unused networks
Conclusion
In Conclusion, Docker network
plays a crucial role in facilitating communication and connectivity between containers within a Dockerized environment.
Several key aspects contribute to the significance of Docker networking:
Isolation and Encapsulation: Docker provides a robust network isolation mechanism, allowing containers to run independently with their own network stack. This isolation prevents conflicts between containers and helps in encapsulating applications and their dependencies.
Efficient Communication: Docker networking enables seamless communication between containers, allowing them to interact and share data. This efficient communication is essential for microservices architectures, where different services collaborate to form a complete application.
Bridge Networks: Docker employs bridge networks by default, which allow containers on the same host to communicate with each other. This internal networking is isolated from the host network, providing an additional layer of security.
User-Defined Networks: Docker allows the creation of user-defined networks, which offer greater control over container communication. This is particularly useful when organizing and structuring complex applications with multiple containers, ensuring better management and scalability.
External Connectivity: Docker containers can be connected to the external network, allowing them to access resources outside the containerized environment. This is vital for applications that need to interact with external services, databases, or the internet.
Port Mapping: Docker enables the mapping of container ports to host ports, making services accessible from the host machine or external systems. This feature is crucial for exposing specific services to the external world while keeping others internal.
In conclusion, Docker's networking
capabilities contribute significantly to the success of containerized applications. The platform's ability to provide isolation, efficient communication, and various networking options makes it a powerful tool for building, deploying, and managing modern, scalable, and distributed applications.
👆The information presented above is based on my interpretation. Suggestions are always welcome.😊
~Smriti Sharma✌