Docker Networking

Sidharth DashSidharth Dash
4 min read
  • In a VM by default docker has bridge network.

  • Types of Docker Networks:--

  • By default docker has three types of networks. Check the hyperlink

    1. Bridge network 2. Host network 3. Overlay 4.ipvlan 5.macvlan 6.none 7 I.P VLAN

      • bridge: It's a default network mode in docker. It creates a private network between host and container, allowing containers to communicate with each other and with the host operating system. How a container can talk to host i.p address by default is bridge network.

      • If you want to secure your containers and isolate them from the default bridge network you can also create your own bridge network.

           docker network create secure-network
        
          ubuntu@ip-172-31-16-65:~$ docker network ls
          NETWORK ID     NAME             DRIVER    SCOPE
          f5a47cca3996   bridge           bridge    local
          9a42e01787a9   host             host      local
          cda7c54ef44c   none             null      local
          eead6d2e0a40   secure-network   bridge    local
        
          docker run -d --name --network secure-network finance nginx:latest
          docker network connect secure-network finance
          docker ps
          docker inspect finance
          docker inspect logout
        
                              "IPAddress": "172.17.0.4",
                              "IPPrefixLen": 16,
                              "IPv6Gateway": "",
                              "GlobalIPv6Address": "",
                              "GlobalIPv6PrefixLen": 0,
                              "MacAddress": "02:42:ac:11:00:04",
                              "DriverOpts": null
                          },
                          "secure-network": {
                              "IPAMConfig": {},
                              "Links": null,
                              "Aliases": [
                                  "47e64f337ad5"
                              ],
                              "NetworkID": "eead6d2e0a40152fa39ca1871625ce3334a90d6c8ee850f6978b9a59e18bdce7",
                              "EndpointID": "900483644ef194fccd77887e5f5afd9829c04e1ea7568343f319b96ca8abe36f",
                              "Gateway": "172.18.0.1",
                              "IPAddress": "172.18.0.2",
                              "IPPrefixLen": 16,
                              "IPv6Gateway": "",
                              "GlobalIPv6Address": "",
                              "GlobalIPv6PrefixLen": 0,
                              "MacAddress": "02:42:ac:12:00:02",
                              "DriverOpts": {}
                          }
                      }
                  }
        
          #suppose i want to check whether i can connect to the secure network 
          #from the host network. Then 
        
          ubuntu@ip-172-31-16-65:~$ docker ps
          CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS     NAMES
          47e64f337ad5   nginx:latest   "/docker-entrypoint.…"   12 minutes ago   Up 12 minutes   80/tcp    finance
          6aa7c42f20d4   nginx:latest   "/docker-entrypoint.…"   27 minutes ago   Up 27 minutes   80/tcp    logout
          0fd7be226650   nginx:latest   "/docker-entrypoint.…"   2 hours ago      Up 2 hours      80/tcp    login
          ubuntu@ip-172-31-16-65:~$ 
          ubuntu@ip-172-31-16-65:~$ 
          ubuntu@ip-172-31-16-65:~$ docker exec -it 0fd7be226650 /bin/bash
          root@0fd7be226650:/# 
          root@0fd7be226650:/# ping 172.18.0.2
          PING 172.18.0.2 (172.18.0.2) 56(84) bytes of data.
          ^C
          --- 172.18.0.2 ping statistics ---
          23 packets transmitted, 0 received, 100% packet loss, time 22513ms
        
          ##It shows i cannt connect to the secure network from the
          #host network
        
      • Host Network:-

        1. It can directly access the container with the host i.p address itself.

          1. There is no custom i.p address of the host network because it's binded with the host networking. Here both container and host fall under same C.I.D.R block.

          2. This mode allows containers to share the host system's network stack, providing direct access to the host system's network.

            To attach a host network to a Docker container, you can use the --network="host" option when running a docker run command. When you use this option, the container has access to the host's network stack, and shares the host's network namespace. This means that the container will use the same IP address and network configuration as the host.

            Here's an example of how to run a Docker container with the host network:

          3. docker run --network="host" <image_name>

          4.   docker run -d --name host-demo --network=host nginx:latest
              docker ps
              docker inspect host-demo
            
      • Macvlan Networking

        Macvlan networks allow you to assign a MAC address to a container, making it appear as a physical device on your network.

      • Commands:--

      • ubuntu@ip-172-31-16-65:~$ history

        1 clear

        2 sudo apt-get update -y

        3 clear

        4 docker pull nginx

        5 ip address show

        6 sudo apt install docker.io

        7 clear

        8 docker ps

        9 sudo usermod -aG docker $USER

        10 docker ps

        11 sudo usermod -a -G docker $USER

        12 docker ps

        13 sudo usermod -a -G docker $USER

        14 clear

        15 docker ps

        16 docker pull nginx

        17 docker images

        18 docker run -d --name login nginx:latest

        19 docker exec -itd login /bin/bash

        20 docker ps

        21 docker exec -it 0fd7be226650 /bin/bash

        22 docker images

        23 docker run -d --name logout nginx:latest

        24 docker ps

        25 docker inspect login

        26 docker inspect logout

        27 docker images

        28 docker ps

        29 docker exec -it 0fd7be226650 /bin/bash

        30 docker network ls

        31 docker network create secure-network

        32 docker network ls

        33 docker run -d --name --network secure-network finance nginx:latest

        34 docker run -d --name --network=secure-network finance nginx:latest

        35 docker run -d --name finance nginx:latest

        36 docker network ls

        37 docker ps

        38 docker network connect secure-network finance

        39 docker ps

        40 docker inspect finance

        41 docker inspect logout

        43 docker exec -it 0fd7be226650 /bin/bash

        44 docker ps

        docker run -d --name host-demo --network=host nginx:latest

        46 docker ps

        47 docker inspect host-demo

        48 history

0
Subscribe to my newsletter

Read articles from Sidharth Dash directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sidharth Dash
Sidharth Dash

Hello Team, Working Devops Support Engineer having 8.2 years of experience in Unix, Shell Scripting, SQL. Since one year i started my journey as a DevOps support engineer where i am involved in Deployments, Infrastructure monitoring using AWS- cloud-watch, Automating simple task using shell scripts.