Sitemap
FAUN.dev() 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Docker Networking

5 min readJan 27, 2023

--

In Continuation with the Docker series ….

What is Networking?

In Business language, Networking is the sharing of information or services between people, or groups. Similarly, In Computers, networking is a way to interconnect applications that can share or exchange data between applications.

In Docker, all the networking is managed by software that installs rules in the host machine so that containers on different networks can/cannot communicate directly with each other.

Press enter or click to view image in full size

Types of Networks in Docker:

1. Bridge

This is the default driver used by docker to launch containers in it. The default Bridge networks are usually used when your applications run in standalone containers that need to communicate.

we can also create user-defined custom bridge networks & these drivers will get more superiority over the default bridge driver. It is also easy to remove or add a running container from the network.

user-defined bridges and the default bridge

User-Defined Bridge networks:

  • containers can resolve each other by name or alias.
  • This provides better isolation in which only containers attached to that network are able to communicate.
  • variable sharing is not possible with user-defined networks using the
    --link option.

Default bridge networks:

  • containers can only access each other by IP addresses unless we use the --linkoption
  • Here any container can communicate with other containers using IPAddress
  • One can share variables using --link option
  1. Check all networks present in Docker:

docker network ls

2. Create a bridge Network:

docker network create --driver bridge mynetwork

3. Assign a network to a running container

docker network connect network_id container_id

4. Remove a container from a network

docker network disconnect network_id container_id

5. Delete a network

docker network rm network_id

2. Host

Sometimes we have a situation where we just want to access something on the host, like some services running on the host & we don’t want to isolate this from host networks.

so, For standalone containers, to remove network isolation between the container and the Docker host, we use host networking.

It is not possible to remove or add a running container to the host network.

If we want to run a container in the host networking stack, we can use the --network host

Run a container in the host network:

docker run -dit --network host --name n1 nginx

3. None

If you want to completely disable the networking stack on a container, you can use the --network none flag when starting the container. Within the container, only the loopback device is created.

Run a container in the None network:

Get DevKishan Joshi’s stories in your inbox

Join Medium for free to get updates from this writer.

docker run -dit --network none --name n1 nginx

4. IPvlan

Suppose we are using a docker container & it is using some port 80 of the host, but we have a requirement that we need to run the app on the same port. so in this case we will be using IPvlan driver.

The IPvlan driver gives users total control over both IPv4 and IPv6 addressing. The VLAN driver builds on top of that giving operators complete control of layer 2 VLAN tagging and even IPvlan L3 routing for users interested in underlay network integration.

Create an IPvlan Network:

First, we have to check the host network details for this use:

ip addr show eth0

Create a network with IPvlan:

docker network create --driver ipvlan --subnet=192.168.1.0/24 --gateway=192.168.1.1 -o ipvlan_mode=12 -o parent=eth0 --name mynetwork

Start a container in the mynetwork :

docker run -dit --name n2 --net mynetwork nginx

5. Macvlan

Some applications, especially legacy applications or applications which monitor network traffic, expect to be directly connected to the physical network. In this type of situation, we can use the macvlan network driver to assign a MAC address to each container’s virtual network interface, making it appear to be a physical network interface directly connected to the physical network. In this case, we need to designate a physical interface on our Docker host to use for the macvlan, as well as the subnet and gateway of the macvlan .

Macvlan is almost similar to IPvlan the extra thing they provide is a unique mac address to each container.

Create a Macvlan Network:

First, we have to check the host network details for this use:

ip addr show eth0

Create a network with IPvlan:

docker network create --driver macvlan --subnet=192.168.1.0/24 --gateway=192.168.1.1 -o parent=eth0 --name mynetwork

Start a container in the mynetwork :

docker run -dit --name n3 --net mynetwork nginx

6. Overlay

The overlay network driver creates a distributed network among multiple Docker daemon hosts. This network sits on top of (overlays) the host-specific networks, allowing containers connected to it to communicate securely when encryption is enabled. Docker transparently handles the routing of each packet to and from the correct Docker daemon host and the correct destination container.

This networking is mainly used by the docker swarm, for this, we need a swarm cluster to be with us.

One can create a cluster here: play-with-docker

On Swarm Manager:

docker swarm init --advertise-addr 192.168.0.17

On worker node (Join swarm cluster):

docker swarm join --token token_received 192.168.0.17:2377

Show all nodes:

docker node ls

Create a network with overlay:

docker network create --driver overlay myoverlay

Start service in the myoverlay network:

docker service create --name mysvc --network myoverlay --replicas 2 nginx

Here we have 2 nodes & 2 replicas have been created on different nodes, but as the network type is overlay so we are able to ping & connect with both replicas.

As you might have gone through the blog I have a task:

Find out & demonstrate how you can share environment variables using --link option in the default bridge network & let me know the commands in the comment section.

Thanks for your Time! Follow & Clap 👏 to keep me Motivated.
Happy Learning!

👋 If you find this helpful, please click the clap 👏 button below a few times to show your support for the author 👇

🚀Join FAUN Developer Community & Get Similar Stories in your Inbox Each Week

--