Docker – networking

These posts contain notes on the Udemy Docker Essentials Course. This post specifically focuses around Section 4, networking in Docker.

root@kalivm:~/section4# docker network create --driver bridge --subnet=10.10.0.0/16 --ip-range=10.10.10.0/24 my-bridge1
4a0e035e505629bc27effcf9e3883f81a8e5eb059d8954bd9508f8b3759f9ed2
root@kalivm:~/section4# docker network ls
NETWORK ID NAME DRIVER SCOPE
9fdbc97ff177 bridge bridge local
8bcf2a4c71ab host host local
06cb1aa4b2e9 my-bridge bridge local
4a0e035e5056 my-bridge1 bridge local
92c41fce1dbd none null local
root@kalivm:~/section4# docker network ls --filter driver=bridge
NETWORK ID NAME DRIVER SCOPE
9fdbc97ff177 bridge bridge local
06cb1aa4b2e9 my-bridge bridge local
4a0e035e5056 my-bridge1 bridge local

It is also possible to inspect the details of a newly created network component such as the bridge created with the previous command.

root@kalivm:~/section4# docker network inspect my-bridge1
[
  {
    "Name": "my-bridge1",
    "Id": "4a0e035e505629bc27effcf9e3883f81a8e5eb059d8954bd9508f8b3759f9ed2",
    "Created": "2019-11-24T10:08:27.138462638+01:00",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "IPAM": {
      "Driver": "default",
      "Options": {},
      "Config": [
        {
          "Subnet": "10.10.0.0/16",
          "IPRange": "10.10.10.0/24"
        }
      ]
    },
    "Internal": false,
    "Attachable": false,
    "Ingress": false,
    "ConfigFrom": {
      "Network": ""
    },
    "ConfigOnly": false,
    "Containers": {},
    "Options": {},
    "Labels": {}
  }
]

To connect a container to a specific network, the connect command can be used

root@kalivm:~/section4# docker network connect my-bridge1 heuristic_benz
root@kalivm:~/section4# docker inspect heuristic_benz
[
----snip----
    "NetworkSettings": {
      "Bridge": "",
      "SandboxID": "d2272688d91b0fd010e28fa90ee01e36de8cb4d8e9291563a09ce6c003f37653",
      "HairpinMode": false,
      "LinkLocalIPv6Address": "",
      "LinkLocalIPv6PrefixLen": 0,
      "Ports": {
        "80/tcp": null
      },
    }
----snip----
    "Networks": {
      "my-bridge1": {
        "IPAMConfig": {},
        "Links": null,
        "Aliases": [
          "9dfe09f03879"
        ],
        "NetworkID": "4a0e035e505629bc27effcf9e3883f81a8e5eb059d8954bd9508f8b3759f9ed2",
        "EndpointID": "4784c0ee80f241ea681dadb40a196dd53cffd8d168ae5cc89cca6bb2f56ff0de",
        "Gateway": "10.10.10.0",
        "IPAddress": "10.10.10.1",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "MacAddress": "02:42:0a:0a:0a:01",
        "DriverOpts": {}
      }
    }
]

When inspecting the network, it can be seen which containers are connected to that specific network

root@kalivm:~/section4# docker network inspect my-bridge1
[
  {
    "Name": "my-bridge1",
    "Id": "4a0e035e505629bc27effcf9e3883f81a8e5eb059d8954bd9508f8b3759f9ed2",
    "Created": "2019-11-24T10:08:27.138462638+01:00",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "IPAM": {
      "Driver": "default",
      "Options": {},
      "Config": [
        {
          "Subnet": "10.10.0.0/16",
          "IPRange": "10.10.10.0/24"
        }
      ]
    },
    "Internal": false,
    "Attachable": false,
    "Ingress": false,
    "ConfigFrom": {
        "Network": ""
    },
    "ConfigOnly": false,
    "Containers": {
      "9dfe09f038798dde14b6ea7e6c792433c42be495a476f02d59644bd7004e349c": {
        "Name": "heuristic_benz",
        "EndpointID": "4784c0ee80f241ea681dadb40a196dd53cffd8d168ae5cc89cca6bb2f56ff0de",
        "MacAddress": "02:42:0a:0a:0a:01",
        "IPv4Address": "10.10.10.1/16",
        "IPv6Address": ""
      }
    },
    "Options": {},
    "Labels": {}
  }
]

And disconnected again

root@kalivm:~/section4# docker network disconnect my-bridge1 heuristic_benz
root@kalivm:~/section4# docker network inspect my-bridge1
[
  {
    "Name": "my-bridge1",
    "Id": "4a0e035e505629bc27effcf9e3883f81a8e5eb059d8954bd9508f8b3759f9ed2",
    "Created": "2019-11-24T10:08:27.138462638+01:00",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "IPAM": {
      "Driver": "default",
      "Options": {},
      "Config": [
        {
          "Subnet": "10.10.0.0/16",
          "IPRange": "10.10.10.0/24"
        }
      ]
    },
    "Internal": false,
    "Attachable": false,
    "Ingress": false,
    "ConfigFrom": {
      "Network": ""
    },
    "ConfigOnly": false,
    "Containers": {},
    "Options": {},
    "Labels": {}
  }
]

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.