What is Docker Networking? |
Understanding docker networking
Docker provided multiple network drivers plugin installed as part of Library along with Docker installation. You have choice and flexibilities. Basically the classification happen based on the number of host participation. SinbleHost WILLLet's see the types of network drivers in docker ecosystem.
Docker Contianers are aim to built tiny size so there may be some of the regular network commands may not be available. We need to install them inside containers.
Issue #2: ping command not working how to resolve this ??
Solution: Go inside your container
To explore docker network object related commands you can use --help
Listing of docker default networks. It also shows three network driver options ready to use :
# There are 3 types of network drivers in Docker-CE Installation with local scoped(Single host) which are ready to use - bridge, host, null. There is the significance in each type of driver.
To check the network route table entries inside a container and on your Linux box where Docker Engine hosted:
we are good to go for validate that from con1 container able to communicate other container con2 ping con2 # Bridge utility if it is not found then install
There is the mapping of IP address shows the bridge attached to container
After looking into the inspect command out you will completely get the idea how the bridge works.
Now let's identify the containers con1 and con2
Now let's see inside the container with the following:
Clean up of all the containers after the above experiment
Now we can get the IP Address of containers of both the networks, using the 'docker inspect' command
Docker overlay network will be part of Docker swarm or Kubernetes where you have Multi-Host Docker ecosystem.
Docker Overlay driver allows us not only multi-host communication, the overlay driver plugins that support it you can create multiple subnetworks.
Docker doc has an interesting Network summary same as it says --
docker network overview |
Docker Contianers are aim to built tiny size so there may be some of the regular network commands may not be available. We need to install them inside containers.
Issue #1 Inside my container ip or ifconfig not working, How to resolve 'ip' command not working?
Solution:apt update; apt install -y iproute2
Issue #2: ping command not working how to resolve this ??
Solution: Go inside your container
apt update; apt install -y iputils-ping
What is the purpose of Docker Network?
- Containers need to communicate with external world (network)
- Reachable from external world to provide service (web applications must be accessable)
- Allow Containers to talk to Host machine (container to host)
- inter-container connectivity in same host and multi-host (container to container)
- Discover services provided by containers automatically (search other services)
- Load balance network traffic between many containers in a service (entry to containerization)
- Secure multi-tenant services (Cloud specific network capability)
Features of Docker network
- Type of network-specific network can be used for internet connectivity
- Publishing ports - open the access to services runs in a container
- DNS - Custom DNS setting Load balancing - allow, deny with iptables
- Traffic flow - application access Logging - where logs should go # check the list of docker networking
To explore docker network object related commands you can use --help
docker network help output |
Listing of docker default networks. It also shows three network driver options ready to use :
docker network ls
# There are 3 types of network drivers in Docker-CE Installation with local scoped(Single host) which are ready to use - bridge, host, null. There is the significance in each type of driver.
docker network create --driver bridge app-net docker network ls # To remove a docker network use rm docker network rm app-netLet's experiment
docker run -d -it --name con1 alpine
docker exec -it con1 sh # able to communicate with outbound responses ping google.com
To check the network route table entries inside a container and on your Linux box where Docker Engine hosted:
ip route or ip r
we are good to go for validate that from con1 container able to communicate other container con2 ping con2 # Bridge utility if it is not found then install
yum -y install bridge-utils# check the bridge and its associated virtual eth cards brctl show
The 'brctl' show command output |
There is the mapping of IP address shows the bridge attached to container
Default Bridge Network in Docker
The following experiment will let you understand the default bridge network that is already available when the docker daemon is in execution.
docker run -d -it --name con1 alpine docker run -d -it --name con2 alpine docker ps docker inspect bridge
default bridge network connected with con1, con2 |
Now let's identify the containers con1 and con2
docker exec -it con1 sh ip addr show ping -c 4 google.com ping -c 4 ipaddr-con2 ping -c 4 con2 --- should not work means dns will not work for default bridges exit# remove the containers which we tested docker rm -v -f con1 con2
User-defined Bridge Network
docker network create --driver bridge mynetwork docker network ls docker inspect mynetwork docker run -d -it --name con1 --network mynetwork alpine docker run -d -it --name con2 --network mynetwork alpine docker ps docker inspect mynetwork
Now let's see inside the container with the following:
docker exec -it con1 sh ip addr show ping -c 4 google.com ping -c 4 ipaddr-con2 ping -c 4 con2 --- should WORKS, that means dns is avaialable in mynetwork exit
Clean up of all the containers after the above experiment
docker rm -v -f con1 con2 #cleanupNow we can check the connection between two user defined bridge networks.
docker network create --driver bridge mynetwork1 docker network create --driver bridge mynetwork2 docker run -d -it --name con1 --network mynetwork1 alpine docker run -d -it --name con2 --network mynetwork1 alpine docker run -d -it --name con3 --network mynetwork2 alpine docker run -d -it --name con4 --network mynetwork2 alpine docker inspect mynetwork1 docker inspect mynetwork2
Now we can get the IP Address of containers of both the networks, using the 'docker inspect' command
docker exec -it con1 sh ping -c 4 ipaddr-con3 #get the ip address using docker container inspect con3 ping -c 4 con3 # This will not work because two bridges isolated
Docker Host networking diriver
Now in the experiment of Host Network example
docker run --rm -d --network host --name my_nginx nginx curl localhostNow you know about how to run a container attached to a network, But now a container created already with default bridge network that can be attach to a custom bridge network is it possible ? is that have same IP address?
How to connect a user-defined network object with running Container?
We can move a docker container from default bridge network to a user defined bridge network it is possible. When this change happen the container IP address dynamically changes.docker network connect command is used to connect a running container to an existing user-defined bridge. The syntax as follows:
Example my_nginx already running container.
Observe after connecting to User defined bridge network check the IP Address of the container.
docker network connect [OPTIONS] NETWORK CONTAINER
Example my_nginx already running container.
docker run -dit --name ng2 \ --publish 8080:80 \ nginx:latest docker network connect mynetwork ng2
Observe after connecting to User defined bridge network check the IP Address of the container.
To disconnect use the following:
docker network disconnect mynetwork ng2Once the disconnected check the IP Address of the container ng2 using 'docker container inspect ug2'. This will concludes that container can be migrated from one network to other without stopping it. There is no errors while performing this.
Overlay Network for Multi-host
Docker overlay network will be part of Docker swarm or Kubernetes where you have Multi-Host Docker ecosystem.
docker network create --driver overlay net-overlay # above command fails with ERROR docker network lsThe overlay networks will be visible in the network list once you activate the Swarm on your Docker Engine. and for network overlay driver plugins that support it you can create multiple subnetworks. #if swarm not initialized use following command with the IP Address of your Docker Host
docker swarm init --advertise-addr 10.0.2.15Overlay Network will serve the containers association with Docker 'service' object instead of 'container' object. # scope of the overlay shows swarm
docker service create --help # Create service of nginx docker service create --network=net-overlay --name=app1 --replicas=4 ngnix docker service ls|grep app1 docker service inspect app1 |more # look for the VirtualIPs in th output
Docker Overlay driver allows us not only multi-host communication, the overlay driver plugins that support it you can create multiple subnetworks.
docker network create -d overlay \ --subnet=192.168.0.0/16 \ --subnet=192.170.0.0/16 \ --gateway=192.168.0.100 \ --gateway=192.170.0.100 \ --ip-range=192.168.1.0/24 \ --aux-address="my-router=192.168.1.5" --aux-address="my-switch=192.168.1.6" \ --aux-address="my-printer=192.170.1.5" --aux-address="my-nas=192.170.1.6" \ my-multihost-network docker network ls docker network inspect my-multihost-network # Check the subnets listed out of this commandThe output as follows:
- User-defined bridge networks are best when you need multiple containers to communicate on the same Docker host.
- Host networks are best when the network stack should not be isolated from the Docker host, but you want other aspects of the container to be isolated.
- Overlay networks are best when you need containers running on different Docker hosts to communicate, or when multiple applications work together using swarm services.
- Macvlan networks are best when you are migrating from a VM setup or need your containers to look like physical hosts on your network, each with a unique MAC address.
Reference:
- Docker Networking: https://success.docker.com/article/networking
- User-define bridge network : https://docs.docker.com/network/bridge/
- Docker Expose Ports externally: https://bobcares.com/blog/docker-port-expose/
No comments:
Post a Comment