DEV Community

Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on

Docker series (Part 7): Create, connect & disconnect your network with a container

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.
Image description

SO, this is actually a network bridge:
Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

Lets inspect the bridge network and see which containers are within this bridge

docker network inspect bridge
Enter fullscreen mode Exit fullscreen mode

Image description

Specially checkout this containers part and you will see that our only running container which is container_5 is connected to this bridge

Image description

We also saw the host network , right? In the network list

Image description
Also, we saw the network None

Image description

SO, these were the networks available . Want to create of your own?

Let's create one

docker network create <network_name>
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

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

Image description

Now, lets inspect our network and see if the container_6 is actually added to it or not

Image description

Look! the container_6 is added with it.
Remember 1 thing, when we inspected the bridge only

Image description
we saw that the container_5 was added to it and its IPV4address was set to this:

Image description

For container_6, it is different and it is

Image description

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

Image description

For, my_app_net, it is this:

Image description
So, why learned this?

Lets create another container and add it with our network my_app_net

Image description
Now, lets inspect the network and look into the containers list

Image description

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>
Enter fullscreen mode Exit fullscreen mode

For us we are using network Ids for my_app_net & container_5

Image description
Now, lets inspect the container_5 and see which networks are attached with it.

Image description

Scroll down and you will find this

Image description
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.

Image description

You can see the, container_5,6 & 7 are attached with it

Image description
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>
Enter fullscreen mode Exit fullscreen mode

Image description

We have used the my_app_net's id & container_5's id to disconnect container_5 from the my_app_net

Image description

Now, lets inspect the container_5 and see the networks it is added

Image description
Scroll down and you will find this

Image description
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

Image description
See! the container_5 is not connected anymore.

Top comments (0)