What is docker networking and bridge networking
Tags: docker, networking, tutorial, devops
Published: 2021-01-01 08:48:18

In docker we can connect two or more containers or non-docker system using networking. Whether it is a windows or Linux containers, docker network helps to connect them in an easy way. This may helpful for beginners who interested in docker.

<I>This post was originally posted on www.developersdoors.xyz</i>
<h2>There are different network drivers available in docker networking</h2>
<ul>
 	<li><code>bridge</code>:
The default network driver if we don't mention a network driver when running a container. It is created automatically when a container is created. It is best for standalone containers</li>
 	<li><code>host</code>:
If you use the host network mode for a container, that container’s network stack is not isolated from the Docker host, and the container does not get its own IP-address allocated.
For instance, if you run a container which binds to port 80 and you use host networking, the container’s application is available on port 80 on the host’s IP address.</li>
 	<li><code>overlay</code>:
Overlay is mainly used when we want to connect multiple containers to create a swarm service to communicate between them.</li>
 	<li><code>macvlan</code>:
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.</li>
 	<li>The other driver is called <code>none</code>, which is used when we use third party plugins from docker hub.</li>
</ul>
<h2>Bridge networking tutorial</h2>
This tutorial explain how a two standalone containers can be connected by bridge network. Here we create two <code>alpine</code> linux containers namely alpine1 and alphine2, then connect them using bridge.
We can view the network list using the command
<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: #f0f0f0; color: #444444;">docker network ls</pre>
You may see different network listed as <code>bridge</code>, <code>host</code>, <code>none</code> as following image. Any newly created network also listed here.
<a href="http://developersdoors.xyz/wp-content/uploads/2020/12/network_ls.png"><img class="aligncenter size-large wp-image-949" src="http://developersdoors.xyz/wp-content/uploads/2020/12/network_ls-1024x408.png" alt="Docker_network_ls" width="840" height="335"></a>
Next create two alpine containers as alpine 1 and alpine 2 with following command
<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: #f0f0f0; color: #444444;">docker run -dit --name alpine1 alpine 
docker run -dit --name alpine2 alpine</pre>
The two container created as follows
<a href="http://developersdoors.xyz/wp-content/uploads/2020/12/contianer_create.png"><img class="aligncenter size-large wp-image-951" src="http://developersdoors.xyz/wp-content/uploads/2020/12/contianer_create-1024x386.png" alt="container_create" width="840" height="317"></a>
Then inspect the bridge network to view details of the containers connected to it with the following command
<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: #f0f0f0; color: #444444;">docker network inspect bridge</pre>
This will display a JSON format of bridge network details. We could see that by default alpine1 and alpine2 has bridge networks with different IP address as <code>172.17.0.2/16</code> and <code>172.17.0.3/16</code> respectively .
<a href="http://developersdoors.xyz/wp-content/uploads/2020/12/network_inspect-2.png"><img class="aligncenter size-large wp-image-959" src="http://developersdoors.xyz/wp-content/uploads/2020/12/network_inspect-2-853x1024.png" alt="network_inspect " width="840" height="1008"></a>
Now connect to the alpine1 container using <code>attach</code> command
<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: #f0f0f0; color: #444444;">docker attach alpine1</pre>
Now attach to the container and ping a website say google.com <code>-c 2</code> make 2 limit of ping.
<a href="http://developersdoors.xyz/wp-content/uploads/2020/12/ping-google.png"><img class="aligncenter size-large wp-image-968" src="http://developersdoors.xyz/wp-content/uploads/2020/12/ping-google-1024x615.png" alt="" width="840" height="504"></a>
Then try to ping the alpine2 container using the IP <code>172.17.0.3/16</code> and check they are connected. If it ping correctly then successfully two containers are connected using bridge.
<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: #f0f0f0; color: #444444;">ping -c 2 172.17.0.3/16</pre>
<a href="http://developersdoors.xyz/wp-content/uploads/2020/12/ping-alpine2.png"><img class="aligncenter wp-image-967 size-large" src="http://developersdoors.xyz/wp-content/uploads/2020/12/ping-alpine2-1024x571.png" alt="docker_networking_bridge" width="840" height="468"></a>
For other docker archive -&gt; <a href="https://developersdoors.xyz/category/programming/docker/">go</a>.
For network reference of docker -&gt; <a href="https://docs.docker.com/network/">go</a>.