Previously we knew that a container generally attached to a bridge . If any traffic comes, it passes through the firewall and then goes to the bridge and then to the container.
SO, this is actually a network bridge:
In part 6, we created a container_5 . And by default it is pushed to this bridge. Lets see the list of networks
docker network ls
Lets inspect the bridge network and see which containers are within this bridge
docker network inspect bridge
Specially checkout this containers part and you will see that our only running container which is container_5 is connected to this bridge
We also saw the host network , right? In the network list
SO, these were the networks available . Want to create of your own?
Let's create one
docker network create <network_name>
You can see that our my_app_net network has been created under the driver bridge . Because bridge is the default driver.
Lets use this network . We will create a container and add it with this network
We will use --network and give our network name
Now, lets inspect our network and see if the container_6 is actually added to it or not
Look! the container_6 is added with it.
Remember 1 thing, when we inspected the bridge only
we saw that the container_5 was added to it and its IPV4address was set to this:
For container_6, it is different and it is
This means that they are not within the same network and can not contact each other.
Also for the bridge, the default subnet & gateway is this
For, my_app_net, it is this:
Lets create another container and add it with our network my_app_net
Now, lets inspect the network and look into the containers list
Here for container_6 , the IPV4 address is: 172.18.0.2/16
for container_7, it is: 172.18.0.3/16
that means they are within the same network. Also, see that the default subnet for this network is: 172.18.0.0/16
Gateway is: 172.18.0.1
That means , any new container will have 172.18.0 in common and depending on vacancies, new will be added. For this network:
Gateway has : 172.18.0.1
container_6 has : 172.18.0.2/16
container_7 has: 172.18.0.3/16
So, this is how they were assigned.
Now, lets do one interesting thing. We have our container_5 added with bridge network. Lets add this with my_net_app network too.
that means that, this container will be attached to the my_app_net and can now easily contact with container_6 & container_7 because they will have a common IP address too.
docker network connect <existing networks id> <existing container's id>
For us we are using network Ids for my_app_net & container_5
Now, lets inspect the container_5 and see which networks are attached with it.
Scroll down and you will find this
See that, it is added with bridge & my_app_net.
Now, lets inspect the network my_app_net and see the containers added with it.
You can see the, container_5,6 & 7 are attached with it
and has IPV4address of : 172.18.0.4/16 , 172.18.0.2/16, 172.18.0.3/16 . Look 172.18.0 is common for everyone and this means that , they can now contact each other.
Also , we can now disconnect the container_5 fro my_app_net .
docker network disconnect <existing network id> <existing container id>
We have used the my_app_net's id & container_5's id to disconnect container_5 from the my_app_net
Now, lets inspect the container_5 and see the networks it is added
Scroll down and you will find this
You can see that, it is only added to bridge network. Not added to my_app_net anymore.
If you want, you can see if this is same for the network itself
Top comments (0)