Docker Neworking
Website Visitors:Docker Network
Docker networking is a crucial aspect of containerization, allowing containers to communicate with each other and the outside world. In this article, we’ll delve into the world of Docker networking, exploring the different types, how to create, modify, and delete networks, and highlighting important points to keep in mind.
-
bridge
: The default network type, which creates a bridge between the host machine and the container. This network is suitable for most use cases. If you don’t specify a driver, this is the type of network you are creating. Bridge networks are usually used when your applications run in standalone containers that need to communicate. See bridge networks. -
host
: This network type allows containers to use the host machine’s network stack, making them appear as if they’re running directly on the host. For standalone containers, remove network isolation between the container and the Docker host, and use the host’s networking directly. See use the host network.In VMware or other hypervisor terminology bridged network means the network on your physical interface and host network means within your VMs only. But it is in reverse in docker.
-
overlay
: Overlay networks connect multiple Docker daemons together and enable swarm services to communicate with each other. You can also use overlay networks to facilitate communication between a swarm service and a standalone container, or between two standalone containers on different Docker daemons. This strategy removes the need to do OS-level routing between these containers. See overlay networks. -
ipvlan
: IPvlan networks give users total control over both IPv4 and IPv6 addressing. The VLAN driver builds on top of that in giving operators complete control of layer 2 VLAN tagging and even IPvlan L3 routing for users interested in underlay network integration. See IPvlan networks. -
macvlan
: Macvlan networks allow you to assign a MAC address to a container, making it appear as a physical device on your network. The Docker daemon routes traffic to containers by their MAC addresses. Using themacvlan
driver is sometimes the best choice when dealing with legacy applications that expect to be directly connected to the physical network, rather than routed through the Docker host’s network stack. See Macvlan networks. -
none
: For this container, disable all networking. Usually used in conjunction with a custom network driver.none
is not available for swarm services. See disable container networking. -
Network plugins: You can install and use third-party network plugins with Docker. These plugins are available from Docker Hub or from third-party vendors. See the vendor’s documentation for installing and using a given network plugin.
Create docker network
To create a Docker network, use the following command:
|
|
Replace <network_name>
with the desired name for your network. You can also specify additional options, such as the driver and subnet, using flags:
|
|
Create a docker container in custom network
Command to create a docker container in custom network: docker run -ti --net NetworkName --name ContainerName ImageName
Ex: docker run-ti --net learning --name UbuntuCustomNetwork ubuntu
- This command will create a container called UbuntuCustomNetwork and add the learning network created above to the container.
Change docker container Network
Command to change network for a docker container:docker network connect NetworkName ContainerNameorID
Ex: docker network connect learning objective_wilson
- This command will connect the network called “learning” to the container called “objective_wilson”.
View Docker Network
docker inspect containerid/name
shows docker container details in which you will find the network name and ip details.
docker network ls
to view all networks in your docker host.
docker network inspect NetworkName
Ex: docker network inspect bridge
- shows bridge network details like subnet etc…
Viewing docker container details along with network
docker ps -a --format '{{ .ID }}\t{{ .Names }}\t{{ json .Networks }}'
- This command shows docker container id, name, and network details.
Modifying a Docker Network
To modify an existing Docker network, use the following command:
|
|
You can update various settings, such as the subnet or gateway, using flags:
|
|
Attach network to a container
docker run --name ubuntu2 --network none ubuntu
creates a container called ubuntu2 from the ubuntu image and adds it to none
network.
Deleting a Docker Network
To delete a Docker network, use the following command:
|
|
Important Points to Keep in Mind
- Network Isolation: Containers on the same network can communicate with each other, but containers on different networks cannot.
- Network Drivers: Docker provides several network drivers, including bridge, host, and overlay. Each driver has its own strengths and weaknesses.
- Subnet and Gateway: When creating a network, you can specify a subnet and gateway to define the network’s IP range and default gateway.
- Container Networking: Containers can be connected to multiple networks, allowing them to communicate with different sets of containers.
- Network Security: Docker networks provide a level of isolation, but you should still implement additional security measures, such as firewalls and access controls, to secure your containers.
In conclusion, Docker networking provides a flexible and powerful way to manage container communication. By understanding the different types of networks, how to create, modify, and delete networks, and keeping important points in mind, you can design and implement a robust and secure containerized infrastructure.
Your inbox needs more DevOps articles.
Subscribe to get our latest content by email.