The Docker containers can easily communicate through the network with other containers and hosts using new network configuration technologies. The network configuration for Dockers is made more convenient with the launching of these new methods. The virtual interface called docker0 is helpful for network configuration setup among Docker containers.
In this chapter, we shall discuss the virtual docker0 interface, how a Docker is configured to local networks, multiple host networking for Dockers, and installation and configuration technologies used with Dockers.
|Table of Content - Network Configuration in Docker|
➤ Configuring Docker to the Network
➤ Simple Ways to Configure Docker to the Local Area Network
➤ NAT for Network Configuration of Docker
➤ Linux Bridges and Components for Docker
While starting of Docker container, it will build docker 0, the virtual kind of interface on the local host system. As per the definition of RFC 1918, the Docker would choose both the subnet and address within the private ranges and then assign it to docker 0. But these addresses must never be used by the host system previously. For example: Imagine that the network mask address is 172.16.42.2/16.
This is capable of supplying 65,534 various addresses for Docker containers and the host system. The network mask address is made up of 16 binary bits. From the IP address of the Docker container, the Media Access Control (MAC) addresses are generated. This is mainly for preventing Address Reservation Protocol collisions. The range that is used for generating MAC address is 02:42:ac:11:00:00 – 02:42:ac:11:ff:ff.
The interface docker 0 is not ordinary. The docker 0 is an Ethernet bridge, virtual, and is used for forwarding network packets automatically through any kind of network interfaces which are attached to docker 0. This enables the communication between Docker containers within its peer groups and also with the host system.
Each time a Docker container gets created, a couple of peer group interfaces are also created. This resembles two sides of a pipe- the data packets that are passed through one end would be received at the other end.
This provides a chance for one among the container peers to turn into ‘eth0’ and this is maintained with a distinctive name such as gehtQSN1DR. This is kept in the host system’s namespace. Each interface of veth* is bound to the bridge of the virtual interface, docker 0. A virtual kind of subnet is shared by Docker between each Docker container and the host system.
|Learn how to use Docker, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Docker Online Training Demo!|
There are four simple ways using which the Docker containers can easily be configured on the local area network. The solutions that are discussed below are not practical but just illustrate a few of the fundamental network technologies that are available with the Linux Operating system.
If any user wishes to use any of the solutions rather than technology demonstrations, he might pay attention to pipework scripting which is used for automating these network configurations.
In the given examples, there is a host having IP address 184.108.40.206 on the 220.127.116.11/20 network. Here we have to create a Docker container that is exposed like: 18.104.22.168. If you run Fedora version 20 along with Docker version 1.1.2, the package utils-Linux is brand enough for including nsenter Docker command. In case you lack that convenient tool, a simpler Docker direction set exists to create the nster command in ipetazzo/nsenter in GitHub Windows installation.
The Network Address Translation or NAT makes use of the normal network model of Docker. The Docker’s network model is mingled with NAT regulations on host system for redirecting the incoming traffic to outgoing traffic from suitable IP addresses.
Allocate the target address for host interfaces:
# -ip -–addr add 22.214.171.124/20 dev em1
In order to initiate the Docker container, use –p option for binding the ports that are exposed to corresponding IP address & the port number on local host.
With the help of above command, the Docker can configure the standard model network:
Since we supplemented –p 126.96.36.199: 60:60 to command line of host, the Docker shall also build the below rule in Docker chain’s nat table that is executed from PREROUTING link.
This resembles the TO traffic with target address as –d 188.8.131.52/31 which is not originating from bridge, docker 0 but directed towards tcp with port number.
You can currently access web server from any local host system connected to network using the matched IP address.
If the Docker container was made to start network connectivity with other system, the connection will be visible using IP address of the local host machine. We shall rectify this by adding SNAT command for POSTROUTING link for changing source IP address.
By using –I POSTROUTING, we can place SNAT rule above POSTROUTING link or chain. By default, the above procedure is needed as Docker already has added below rule above POSTROUTING link:
-A POST-ROUTING –s 184.108.40.206/15 ! -d 220.127.116.11/15 -j MASQUERADE
The above rule, ‘MASQUERADE’ will match the traffic with any Docker container as well as we must place our above rule as earlier as possible in POSTROUTING for making few affect.
Using these kinds of rules, the traffic to IP: 18.104.22.168 (port number: 80) is being directed to the web container and also the traffic that originates from web container must be appearing as if it came from 22.214.171.124.
|Related Article: Docker Interview Questions and Answers Experienced|
The past example of configuration is simpler but with few restrictions. If you are configuring a network interface with the help of DHCP or there is any application that is needed to run on a similar layer two domain broadcast like any other systems on the local network, the rules of NAT will not be working.
The process makes use of the device with a Linux bridge, which is created with the help of brctl and helps in connecting the Docker containers with physical networks directly.
We will be adding em2 to the new bridge and shifting the IP address to the bridge from em2. Cautions to be followed: The first-time configuration is not done remotely and keeping this permanent will vary for each distribution thus it will never be a persistent kind of configuration.
This supplies the standard interface, eth0 inside Docker container, but we need to ignore this and add a different one.
This is closely similar to the previous method but we make use of Open vSwitch for Linux bridges. The below commands are given with the assumption that Open vSwitch is already configured in your host system.
Build an Open vSwitch (OVS) bridge with the help of ‘ovs-vsctl’ command.
The next step is adding an external interface.
Continue as per the previous instruction set.
Caution: The OVS network configuration will persist during rebooting. This is when the host system backs up, em2 can still be br-em member which will result in lack of network connection for your local host.
Assure ovs-vsctl del-port br-em2 em2 ahead of your system reboot.
Even this method is similar with previous other operations but for using bridges, we can create macvlan that is virtual bridge network interface that is connected through physical network interface. Unlike those two solutions, the current method never requires any disturbances for your chief networking interface.
Begin by building Docker container as like previous cases:
Build interface, macvlan united with physical interfaces:
This will create recent interface, macvlan namely ‘em2p1’ that is associated along with em2 interface. We set up this using bridge mode that will permit all the macvlan interfaces for communicating with one another.
For namespace of network container, add the new interface:
The host system is not able to make communication with macvlan machines through primary interfaces. You could build other macvlan interfacing on host device. Give an appropriate address on network and set routing for your Docker containers through interface:
It is common to run multiple Dockers nowadays. There are many guidelines that are present online for getting started and enable you to create Docker containers in box- it may either be Mac or Linux server in projects similar to boot2docker.
There are various options available to run Dockers for multiple boxes:
Fundamentally, the following sequence of steps is performed:
A complete mesh network is obtained while doing service restart or server reboot, using connection redundancy. The Docker assistance service is capable of spawning containers on appropriate IP address range, avoiding overlapping. This will connect with each other lacking exposure of all ports on private or public interfaces.
This is the quick glimpse of major technologies that we are using:
Let us consider that the servers are running Ubuntu Server version 14.04.02 LTS x64. You can adapt various kinds of configuration methods as follows for other operating systems.
Using this technology, you can follow various guidelines as found on official website. Let us see various types of Docker configuration and services later in this chapter.
The OpenVSwitch or OVS packages are not available with default or outdated repositories. We are now building .deb files and dispose it on various kinds of hosts. For maintaining prod boxes tidy, get a small box for installing and building dev packages.
All the guidelines for building are made available on Github’s OpenVSwitch.
For creating your own packages, perform the below, adapting to newer versions if any:
You can be creating meshed networks with several CLI devices of OpenVSwitch and Ubuntu gives you a helper for defining the network via /etc/network/interfaces. Let us consider various three servers: 126.96.36.199, 188.8.131.52 and 184.108.40.206. These servers can ping with each other with the help of these IP addresses and can either be private or public.
This configuration must be altered to use with other host systems. The IP address sets of remote_ip must be paired appropriately.
Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!
|Docker Training||Jun 10 to Jun 25|
|Docker Training||Jun 13 to Jun 28|
|Docker Training||Jun 17 to Jul 02|
|Docker Training||Jun 20 to Jul 05|
Vinod M is a Big data expert writer at Mindmajix and contributes in-depth articles on various Big Data Technologies. He also has experience in writing for Docker, Hadoop, Microservices, Commvault, and few BI tools. You can be in touch with him via LinkedIn and Twitter.
Copyright © 2013 - 2023 MindMajix Technologies