DEV Community

technonotes-hacker
technonotes-hacker

Posted on

Kubernetes - Day - 06 - Network Inside Docker

Different types of Network

Why we need to speak about network ?

  1. How a container speak to anothe container ?
  2. How a container connect to internet ?

To answer all these questions we need to study different types of Network.

Bridge Network ( default )

  • docker0 ( which is the default bridge )
  • 172.17.0.0/16 172.17 --> Blocked to Network 0.0/16 --> Available to the Host
  • Uses IP tables
  • docker 1 runs nginx & docker 2 runs postgres.
  • Default bridge --> you can hit docker 1 to the other docker by IP not by its name.
  • Container gets random IP's.
  • They can't ping each other by name ( no DNS ), only by IP address. Because of this custom network exists.

docker run -d --name web_nginx nginx
docker run -d --name db_test postgres
docker inspect web_nginx

Bridge Network ( Custom )

docker run -d --name web_nginx --network mynetwork nginx
docker run -d --name web_nginx --network mynetwork postgres
docker exec -it web_nginx bash
docker inspect db_test

  • Now we can see DNS in the inspect command.
  • Inside my give network , i can speak via Domain Name. No need to know the IP. (this is advantage of the custom Bridge Network).
  • Every user defined network gets automatic DNS.
  • Containers reach each other by NAME, not by IP which is the recommended setup.

Host Network

  • The container shares the host IP & port space entirely.
  • Fasted option but no isolation.
  • Port clashes are possible.
docker run -d --network **host** nginx ( host --> its runs as if like host )
Enter fullscreen mode Exit fullscreen mode
  • Instead of creating a separate environment , just run in my host itself.
  • It will run as a process or a software.
  • There won't be any IP allocated.

None

  • You run a container , it can't communicate to the outer world.
  • It can used to run Any task.
  • No request can be sent inside.
  • It will be manily used for Job / cron tab.
  • Its an isolated machine.
  • Volume mount can be done and necessary packages can be run as container.
docker run -it --network none alphine bash/sh
docker run -it --network none python:3.10.21-alphine3.23
Enter fullscreen mode Exit fullscreen mode

Overlay

  • Its like tunnle.

MacVLan

  • A conatiner gets a REAL IP & MAC address on the physical network.
  • Other networks won't have MAC address , only MacVLan will have the MAC address.
  • It will be like " ஒரு laptop பக்கத்துல இன்னொரு laptop இருக்கிற மாதிரி ".
ip addr ( check the IP of the machine ) Eg., 192.168.1.5/24
docker network create -d macvlan --subnet=192.168.1.0/24 --gateway=192.168.1.1 -0 parent=wlp2s0 macvlan
docker run -d --network macvlan_net
docker exec -it <number> bash
docker inspect <number> ( IP will be 192.168.1.2 )
Enter fullscreen mode Exit fullscreen mode
  • Eg., It will be used for legcy system to communicate eacf other.

Commands

docker rm -f $(docker ps -aq)
docker run -d --name web_nginx nginx
docker run -d --name db_test postgres
docker inspect web_nginx ( it will show the default bridge network )
docker logs -f db_test
docker network ls ( list the set of networks available )
docker network create mynetwork ( its takes the default Bridge network )
docker run -d --name web_nginx --network mynetwork nginx
docker run -d --name web_nginx --network mynetwork postgres
Enter fullscreen mode Exit fullscreen mode

Notes

  • What is Redis ? Redis is a free, open-source, in-memory data structure store used as a database, cache, message broker, and streaming engine.

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.