- Here we have 2 containers and 1 is front end container and 2nd is backend container and this 2 containers should communicate with each other.
- Frontend -- should talk to backend.
- There should be a networking way to talk to each other.
- Any container will definitly talk to the host be'coz containers are package or bundle which don't have complete operating systems. Subnet : Basically a networking group
- 2 Containers 1 is Login and other payment, which are completely isolated that means, that they are not is same network, they don't talk to each other.
what does a docker container will talk to the system or host
--
- By default we have eth0 network Ex: 192.168.138.156, It will be created by default by any resources that you are creating that as container or VM.
- Docker conatiner runs of 172.17.0.2 eth0
- If we try to ping from container 172.17.0.2 to 192.168.138.156 host machine. The ping will not work we will get network error.
- WIthout this virtual network conatiner can not talk to the host. And this is called Bridge network
- If we try to docker this bridge network then the container will not talk to host
- Bridge network
- Host networking --- Containers will directly use the host. In this case docker will directly bind ip address of the host not exactly ip address eth0 of the host, when we are creating container. -- Host has 192.168.138.4 and conatiner as 192.168.138.6, both of them are in same subnet. Bydefault we y ou try to ping the conatiner to host, it is possible. -- Whoever have the access to host can also have access to container.
- Overlay networking -- Mostly used in k8s or docker swarm
- Container 1 is login container and container 2 is the finance container. The finance container is completely isolated or as much as secure as compare with login container.
- But if we use the default network. there is only one eth0 docker 0 the container 2 finance conatiner will also use the same network, and both of them are using the same virutal network to talk to the host.
- This is the problem and it is not secure.
- For docker run command we can pass this as --network
- Container -- custom network -- eth0
- sudo docker run -d --name login nginx:latest
install iputils-ping to ping from login to logout container ping is working
- sudo docker run -d --name logout nginx:latest
- sudo docker inspect login
- sudo docker network create secure-network (secure-network is network name)
- sudo docker network ls (To check the networks)
- sudo docker run -d --name finance --network=secure-network nginx:latest (To run with custom network)
- sudo docker inspect finance
- Now we are not able to ping this conatiner with other container, because it secure and isolated.
-
Host network is nothing but pc or vm or instancce network
-
Container will be created with no ip address if we inspect we don't get any ip address
-
It has same ip address as our system ip
-
sudo docker run -d --name host-demo --network=host nginx:latest
-
sudo docker inspect host-demo