Docker Networking

Docker Network

Also, check if you need to know about docker0 and etho.

Docker supports networking for its containers via network drivers. These drivers have several network drivers.

The Bridge Driver

Let’s see in action

$ docker network ls

We will create two nginx containers and see which network it attaching to.

$ docker run -itd --name nginx1 nginx$ docker run -itd --name nginx2 nginx$ docker ps $ docker network inspect bridge |grep Name

Here, both Nginx containers are attached to the bridge network.

The Host Driver

$ docker network ls

Create an httpd container in host networking

$ docker run -itd --net host --name host_web httpd

To check the container created within-host network

$ docker network inspect host |grep Name

The IP address of the container within-host network is null.

$ docker network inspect host |grep IPv4Address

Access the web container with Public IP.

Overlay Driver

$ docker network ls

You don’t see any overlay network driver created by docker and can’t create it for the common use of docker container.

Let try to create it.

$ docker network create -d overlay myStack1

In order to use the Overlay network, we have to use the Swarm service. we will see the Docker Swarm section.

Macvlan Driver

docker network ls

You don’t see any macvlan network driver created by docker.

Create MACVLAN network “mvnet” bound to eth0 on the host.

$ docker network create -d macvlan --subnet 192.168.0.0/24 --gateway 192.168.0.1 -o parent=eth0 mvnet

Create two containers C1 and C2 on the “mvnet” network and ping the C1 from C2 with the container’s C1 IP address.

$ docker run -itd --name C1 --net mvnet --ip 192.168.0.3 busybox sh
$ docker run -it --name C2 --net mvnet --ip 192.168.0.4 busybox sh
$ ping 192.168.0.3

In the above example, we have created a macvlan network eth0 on the host and also attach two containers to the macvlan network and show that they can ping between themselves with the container’s IP.

None Driver

$ docker network ls

Basic Docker Networking Commands

List down the Networks associated with Docker

$ docker network ls

Creating a Network

$ docker network create mynetwork

Disconnecting a Container from the Network

$ docker network disconnect mynetwork 0f8d7a833f42

Displays detailed information on one or more networks.

$ docker network inspect mynetwork 

Remove all Unused Networks

$ docker network prune

Cloud and DevOps Enthusiast