Docker Networking

  In Docker, if 2 containers communicate to each other, it means they are in-network. Find all the IP addresses on a system Command :- ip a ...

 In Docker, if 2 containers communicate to each other, it means they are in-network.

Find all the IP addresses on a system

Command:- ip a

Find all the networks in docker

Command: docker network ls

Types of Networks in Docker


It is the default network (docker0) in docker, which means if a container is created by default it is created on top of bridge network docker0.

If 2 or more containers get created on the bridge network then they are automatically in the same network, which means they can communicate with each other.

Step 1:- Create container c1 with ubuntu image

             docker container run -it --name c1 -d ubuntu

Step 2:- Check c1 container is running on the docker0 network

              ip a

             you will find one veth....on docker0

             Also, check the CIDR for docker0

Step 3:- Check the IP address of the container, it should be in the CIDR range of the docker0 network. In my case, IP address of c2 is ""

             docker container inspect c1

Step 4:- Create container c2 with centos image

             docker container run -it --name c2 -d centos

Step 5:- Check c2 container is running on the docker0 network

              ip a

             you will find one more veth....on docker0

             Also, check the CIDR for docker0

Step 6:- Check the IP address of the container, it should be in the CIDR range of the docker0 network. In my case IP address of c2 is ""

             docker container inspect c2

Step 7:- Check container c2 ping to c1. It should get a reply from c1 because both are in the same network.

            docker exec -it c2 bash


Custom Bridge Network or User Define Bridge Network

When a network which is created by user or sysadmin so that specified containers can run on it then it is custom bridge network.

Step 1: List all the containers

          docker network ls

Step 2: List all the commands related to docker network

          docker network --help


  1.   connect:-     Connect a container to a network
  2.   create          Create a network
  3.   disconnect:-  Disconnect a container from a network
  4.   inspect          Display detailed information on one or more networks
  5.   ls          List networks
  6.   prune       Remove all unused networks
  7.   rm          Remove one or more networks


Step 3: Create a Bridge network br1

            docker network create -d bridge br1

Step 4: Verify network is created successfully. It should be listed in docker networks.

            docker network ls

Step 5: Inspect br1 network to find more detailed information.

            docker network inspect br1

Note: You can create a bridge network with your own subnet

docker network create -d bridge --subnet= --gateway= br2

It means if the containers get created on this network layer then they have IP addresses in range.

Step 6:- Create a container on top of br1 network

docker container run -it --name c1 --network br1 -d ubuntu

Step 7: Inspect the container and it should have IP address within the br1 CIDR range.

  docker container inspect c1

Step 8: Remove the container and network.

  docker rm -f c1

  docker network rm br1

  docker network ls

Challenge: Create a bridge network and run a web app container which stores the data in a database container.

Docker images:

web app :- ramansharma95/webapp

db :- ramansharma95/mysql

webapp :- This image is having a webserver which apache and index.php file is hosted on this server (/var/www/html/index.php) and it is a simple webform to enter details in the database container.

db:- This image is used as database container which stores the data via webapp. Database container's details should be the part of webapp connection string.

db Image is developed in mysql. For webapp container we need to create a database called company and a table in the company database called employee which has name,mobile fields

Step 1:- Create a custom docker bridge network called webnetwork

docker network create -d bridge --subnet= --gateway= webnetwork

docker network ls

Step 2: Download required images

           docker pull ramansharma95/webapp

           docker pull ramansharma95/mysql

Step 3: Create a webapp container and do a port forwarding on port number 80

docker container run -it --name web --network webnetwork -p 80:80 -d ramansharma95/webapp

Goto browser and check that you are able to see the default web page. localhost:80

Step 4: Go inside the container and check the code for index.php

  docker exec -it web bash

   cd /var/www/html/

  cat index.php

Step 5:- Create db container

        docker container run -it --name db --network webnetwork  -d                     ramansharma95/mysql

Step 6:- Go inside the container.

        docker exec -it db bash

  connect to mysql with username root and password whizlabs 

       mysql -uroot -pwhizlabs

   Create a database company

       show databases;

       create database company;

       show databases;

   Create a table employee with name and mobile field.

       use company;

       create table employee ( name varchar(30), mobile varchar(30));

   Show all the records in this table

      select * from employee;

Step 7:- Add some employees details in the webpage and check again records in employee table, it should have those records added.

      Show all the records in this table

      select * from employee;

Host Network

When the containers run on top of host mahcine network then they have same IP address as the host machine IP a

Step 1: create an nginx container on top of host network

docker container run -it --network host --name h1 -d nginx

Step 2: Check on the browser that nginx page is opened, no port forwarding is required.


Step 3: Inspect the container to check that it is created on the host network

        docker inspect h1

Repeat webapp and db container exercise for host network.

None Network

When no IP address is assigned to the container you can run the container in none network. It is mostly used for applications that need to test in an isolated environment. 

Step 1: Create a centos container on none or null network

 docker container run -it --name n1 --network none -d centos

Step 2: Inspect the container and verify that it is running on none networks

            docker inspect n1

Step 3: Once the testing is done then remove none network from n1 container and attach bridge network

           docker network disconnect none n1

           docker network connect bridge n1

Step 4: Verify the n1 container is having bridge network

           docker inspect n1





Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SDLC Quiz,3,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
DevOpsWorld: Docker Networking
Docker Networking
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content