Docker Networking

Docker Networking
Container Network Model(CNM) – Specification
libnetwork – CNM Implementaion
Drivers – Bridge Driver, Overlay Driver, MACVLAN
Defult network : none, host, bridge

docker network –help
docker network ls
docker network inspect bridge
docker network create frontend
docker container run -d –name webserver1 -p 8081:80 nginx
docker container inspect webserver1
By default while a container create it will be attached with default network bridge

docker network ls
docker ps -a
docker network connect
docker network connect frontend webserver1
docker network inspect 685(ID/name) //check container name
docker container inspect 457(ID/name) //check network name, it will have already default network attached “bridge” additionaly another network name will be available in object
docker network disconnect
docker network disconnect frontend webserver1
docker container inspect webserver1 // frontend network will be disconnected
docker network rm frontend (remove network)
docker network prune

None :
If we want to create a container , it has to be isolated we can give network as none. It will not have ip address but we can execute container & interact
Truns off networking and is useful for two cases :
Containers that don’t need a network, such as batch jobs writing to a disk volume, or if you want to set up your own custom networking
docker container create –network=none

Bridge:
-Bridge is the default network driver for Docker
-If we dont speicfy a driver, this is the type of network you are creating
-We also can create a User-Defined Bridge Network which is superior to the default bridge

-Docker daemon creates a bridge on the host with the name of docker0
-When a container launches, Docker thn creates a virtual ethernet device for it
-This device appears within the container as eth0 and on the host with a name like vethxxx where xxx isa unique identifier for the interface.
-The vethxxx interface is added to the docker0 bridge, and this enables communication with other containers on the same host that also use the default bridge.

docker run -id –name busybox-1 busybox
docker inspect busybox-1
docker network inspect bridge (while create container it is associated into bridge network)

Communication :
docker run -id –name alpine-1 alpine ash
docker run -id –name alpine-2 alpine ash
docker run -id –name alpine-3 alpine ash

If your container is running , Attach to Your Existing Container
docker attache alpine-1 (get into container)
/# ip addr show
if you got command not found error
If you need to use an existing container and the ping utility is not installed, you need to install it first.

Run the following commands inside the container :
apt-get update
apt-get install -y iputils-ping

Run the Ping Command Inside the Container:
ping -c 4 google.com (Container to outside communication)

Running a New Container with Ping
If you prefer to run a new container each time for testing purposes:

docker run –rm alpine ash -c “apt-get update && apt-get install -y iputils-ping && ping -c 4 google.com” (Use Case for –rm)

Get the IP Address of the First Container from host :
docker inspect -f ‘{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ alpine-1

//Output : ‘172.17.0.2’

Ping the First Container from Another Container:(Container to Container communication)
1. attach into one container
2. ping -c 3 ‘172.17.0.2’

docker run –rm alpine ping -c 4 (both container should be in same OS ubuntu/alpine)
docker run –rm alpine ping -c 4 172.17.0.2 (Use Case for –rm)

Running a Temporary Container:
docker run –rm alpine ping -c 4 google.com
After the ping command completes, the container will automatically be removed.

Use Case for –rm:
Temporary Tasks: When you need to run short-lived tasks and don’t need the container afterward.
Cleanup: Helps in automatically cleaning up containers that are not needed after their task is done, saving disk space and keeping the environment tidy.

Without –rm
If you omit the –rm flag, the container will remain in a stopped state after it finishes its task, which you can see with docker ps -a. You would need to manually remove such containers using docker rm .
docker run ubuntu ping -c 4 google.com

docker run -dit –name alpine-2 alpine ash

docs.docker.com/network/nework-tutorial-standalone

Leave a Reply

Your email address will not be published. Required fields are marked *