Docker series (Part 7): Create, connect & disconnect your network with a container
Tags: docker
Published: 2022-06-09 06:40:10

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ascd6kw67q1qs9fi0lwd.png)
 
SO, this is actually a network bridge:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27ur3lc5npl5j24vvy8r.png)

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
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ama9dhb1j601fi19egzj.png)
 
 Lets inspect the bridge network and see which containers are within this bridge

```
docker network inspect bridge
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z99lrbb1c13cxpyrzchd.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2we2tvg0x3v0cxnetoqg.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b389zkt8gzgcf56vfnil.png)
Also, we saw the network None

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lagr42skg77vqqd7klb9.png)

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

Let's create one

```
docker network create <network_name>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/poj8z6es5gm4kjqa5pro.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xp7q4lpd4n8hit6qlb46.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wdbm54mqfljhe7c0q13.png)
 
 
Now, lets inspect our network and see if the container_6 is actually added to it or not


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sd2phdwrqom012x1alnc.png)

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

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nsby8cigyjk4ze4ofhmt.png)
we saw that the container_5 was added to it and its IPV4address was set to this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqg7q9ewij5nycw9vboe.png)

For container_6, it is different and it is 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25vn9znz71vcd3ggcpn8.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/immlx2aw3sh120hpec03.png)

For, my_app_net, it is this:

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xl3jgiy0amehdw35i868.png)
So, why learned this?

Lets create another container and add it with our network my_app_net

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f496osiw62bt7ajziy0c.png)
Now, lets inspect the network and look into the containers list

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dk6jdvwizoxrbtlfi2vz.png)
      
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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzm3tevr5yhfgmsd4dd8.png)
Now, lets inspect the container_5 and see which networks are attached with it.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik50kn6vvtmxhgfxbsv5.png)

Scroll down and you will find this


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhffzeq9evibw4iovxsf.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pl8g00o5gvol6lf3ahxi.png)

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

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fyn8y10j0ny01z7ozyz5.png)
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>
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44jj5nr1pv7600ya2256.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iv37ythz3bykcn8vhxxi.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iugzfs4oz1oxnqkl6gf0.png)
Scroll down and you will find this

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2jwhcnbjvl9ywi6jdpf.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9x46tcvsxwro9xqk15y.png)
See! the container_5 is not connected anymore. 


<img src="https://c.tenor.com/Tvd3VKwtse0AAAAC/cat-cute.gif">