Wednesday, March 16, 2022

Module 6: Docker DNS ( Domain Name Service)

DNS services
By default, a container inherits the DNS settings of the host, as defined in the /etc/resolv.conf configuration file. Containers that use the default bridge network get a copy of this file, whereas containers that use a custom network use Docker’s embedded DNS server, which forwards external DNS lookups to the DNS servers configured on the host.

➤ DNS stands for Domain Name System. 

➤ DNS is how domain names are translated into IP addresses. DNS is what allows you to use your web browser to find web sites. 

➤ DNS makes it possible for us to use easy to remember domain names in place of complex IP addresses.

➤ Containers uses DNS to communicate. 

➤ Containers don’t use IP address to Communicate.

docker network create -d bridge mynetwork

The -d flag tells Docker to use the bridge driver for the new network. You could have left this flag off as bridge is the default value for this flag. Go ahead and list the networks on your machine:

$ docker network ls

NETWORK ID          NAME                DRIVER
bb9dc0e96308   bridge      bridge    local
2435e354c788   host        host      local
b451a848eecb   mynetwork   bridge    local
987819fa19d0   none        null      local
Then Inspect the network 
docker network inspect mynetwork
[
    {
        "Name": "mynetwork",
        "Id": "d2c2870a549606fdc8aa47c6d06e6d2358bebcd3806f5428b33d3fa69abc5cad",
        "Created": "2022-02-26T22:03:13.255007777Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

Let's create two containers to run on mynetwork 
root@dockerboss:~# docker run -d --name my_nginx1 --network mynetwork nginx:alpine

start another container with my_nginx2
root@dockerboss:~# docker run -d --name my_nginx2 --network mynetwork nginx:alpine

Then let us see all the containers that are running with docker ps -a

root@dockerboss:~# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS                                   NAMES
971e6c87abe7   nginx:alpine   "/docker-entrypoint.…"   About a minute ago   Up About a minute   80/tcp                                  my_nginx2
02f9f07eb7d7   nginx:alpine   "/docker-entrypoint.…"   3 minutes ago        Up 3 minutes        80/tcp                                  my_nginx1
8872e2ec486f   nginx          "/docker-entrypoint.…"   14 minutes ago       Up 14 minutes       0.0.0.0:8080->80/tcp, :::8080->80/tcp   affectionate_blackburn
Then let us inspect the network again with docker network inspect mynetwork 
root@dockerboss:~# docker network inspect mynetwork
[
    {
        "Name": "mynetwork",
        "Id": "d2c2870a549606fdc8aa47c6d06e6d2358bebcd3806f5428b33d3fa69abc5cad",
        "Created": "2022-02-26T22:03:13.255007777Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "02f9f07eb7d73f9aef7a206f1f8a159319f1194dd26131145a3ba9b21fa9b248": {
                "Name": "my_nginx1",
                "EndpointID": "67dea7f43119a77a82344b68bace369a12f350b7b859408841d169a487d45a80",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            },
            "971e6c87abe7d4c54ade03941e60b6889509eb670580e04e96c82adfc8eb7fd2": {
                "Name": "my_nginx2",
                "EndpointID": "7236d22c29151425bc1ba9aa546adc39155073a21b7e692b82ab97695f398077",
                "MacAddress": "02:42:ac:13:00:03",
                "IPv4Address": "172.19.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
root@dockerboss:~#

Then let's try to ping my_nginx1 from my_nginx2 
root@dockerboss:~# docker exec -it my_nginx1 ping my_nginx2
PING my_nginx2 (172.19.0.3): 56 data bytes
64 bytes from 172.19.0.3: seq=0 ttl=64 time=0.103 ms
64 bytes from 172.19.0.3: seq=1 ttl=64 time=0.086 ms
64 bytes from 172.19.0.3: seq=2 ttl=64 time=0.083 ms
64 bytes from 172.19.0.3: seq=3 ttl=64 time=0.085 ms
64 bytes from 172.19.0.3: seq=4 ttl=64 time=0.084 ms
64 bytes from 172.19.0.3: seq=5 ttl=64 time=0.086 ms
64 bytes from 172.19.0.3: seq=6 ttl=64 time=0.084 ms
64 bytes from 172.19.0.3: seq=7 ttl=64 time=0.088 ms
64 bytes from 172.19.0.3: seq=8 ttl=64 time=0.083 ms
64 bytes from 172.19.0.3: seq=9 ttl=64 time=0.087 ms
64 bytes from 172.19.0.3: seq=10 ttl=64 time=0.097 ms
64 bytes from 172.19.0.3: seq=11 ttl=64 time=0.086 ms
^C
--- my_nginx2 ping statistics ---
12 packets transmitted, 12 packets received, 0% packet loss
round-trip min/avg/max = 0.083/0.087/0.103 ms
Then why we do not use ip address because it is not static and when you stop this container and re-start it again, the chance that same container may not have same ip address to connect 
so let us stop my_network1 which has 172.19.0.2 
root@dockerboss:~# docker stop my_nginx1

Then start a new container my_nginx3 on same network mynetwork 

root@dockerboss:~# docker run -d --name my_nginx3 --network mynetwork nginx:alpine

so let us inspect the network to see the ip address that was assign to my_nginx3

root@dockerboss:~# docker network inspect mynetwork
[
    {
        "Name": "mynetwork",
        "Id": "d2c2870a549606fdc8aa47c6d06e6d2358bebcd3806f5428b33d3fa69abc5cad",
        "Created": "2022-02-26T22:03:13.255007777Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "971e6c87abe7d4c54ade03941e60b6889509eb670580e04e96c82adfc8eb7fd2": {
                "Name": "my_nginx2",
                "EndpointID": "7236d22c29151425bc1ba9aa546adc39155073a21b7e692b82ab97695f398077",
                "MacAddress": "02:42:ac:13:00:03",
                "IPv4Address": "172.19.0.3/16",
                "IPv6Address": ""
            },
            "f38c47ea5a38ff350f3d3221c0bffdddf1be549e9f1ad3a6ab70a7155f72a3cc": {
                "Name": "my_nginx3",
                "EndpointID": "13af56e584d08f0f0ec6e4f75776ac68be75f032c3e873312bab28b8a7c582f2",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
That is true to see that my_nginx3 container was assigned the ip address that my_nginx1 was work assigned earlier
Let start my_nginx1 with docker start my_nginx1
 and inspect the network again to see the new ip address for my_nginx1


[
    {
        "Name": "mynetwork",
        "Id": "d2c2870a549606fdc8aa47c6d06e6d2358bebcd3806f5428b33d3fa69abc5cad",
        "Created": "2022-02-26T22:03:13.255007777Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "02f9f07eb7d73f9aef7a206f1f8a159319f1194dd26131145a3ba9b21fa9b248": {
                "Name": "my_nginx1",
                "EndpointID": "e58148da92df9f9576e2badb04f83067b67b224e4ba04d8b243ed6b60dcc2b97",
                "MacAddress": "02:42:ac:13:00:04",
                "IPv4Address": "172.19.0.4/16",
                "IPv6Address": ""
            },
            "971e6c87abe7d4c54ade03941e60b6889509eb670580e04e96c82adfc8eb7fd2": {
                "Name": "my_nginx2",
                "EndpointID": "7236d22c29151425bc1ba9aa546adc39155073a21b7e692b82ab97695f398077",
                "MacAddress": "02:42:ac:13:00:03",
                "IPv4Address": "172.19.0.3/16",
                "IPv6Address": ""
            },
            "f38c47ea5a38ff350f3d3221c0bffdddf1be549e9f1ad3a6ab70a7155f72a3cc": {
                "Name": "my_nginx3",
                "EndpointID": "13af56e584d08f0f0ec6e4f75776ac68be75f032c3e873312bab28b8a7c582f2",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

To resolve this problem , docker uses container  dns  instead of container ip 

No comments:

Post a Comment

Install SonaType Nexus 3 on Ubuntu 24.0.4 - How to configure SonaType Nexus 3 on Ubuntu - Install Nexus on Ubuntu

SonaType Nexus3 is one of the popular binary repository managers, used for storing build artifacts such as Jars,WARs, EARs. It is Java based...