Mutual calls between docker containers

Record the invocation of two applications of the docker container during the data migration process. In
the process of data migration, a new docker container needs to be created, but the container will be created. Check the log and find that the passport container has no way to call the contacts container, and the container is not implemented. call. (It is found that they are called by the container name, and then pinging the contacts container name in the passport container is unreasonable)

[root@zfl ~]# docker exec -it passport bash
root@70a9e5d670c6:/# ping contacts
bash: ping: command not found
root@70a9e5d670c6:/# curl contacts
curl: (6) Could not resolve host: contacts
root@70a9e5d670c6:/# 

record solution

1. Method through domain name resolution (temporary method)

This approach does not apply to future extensions

1.1 Add contacts domain name resolution in passport

1.1.1 View the ip of contacts

[root@zfl ~]# docker inspect contacts |egrep -i ipadd
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.5",
                    "IPAddress": "172.17.0.5",

1.1.2 Add domain name in passport

root@70a9e5d670c6:/# echo 172.17.0.5 contacts >>/etc/hosts
root@70a9e5d670c6:/# cat !$
cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.6	70a9e5d670c6
172.17.0.5 contacts
root@70a9e5d670c6:/# 

root@70a9e5d670c6:/# curl contacts
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html {
    
     color-scheme: light dark; }
body {
    
     width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@70a9e5d670c6:/# 

1.2 Create containers directly through add-list

1.2.1 First create the called container

[root@zfl ~]# docker run -itd --name contacts -p 800:80 nginx:latest 
e8c5a9008a6298685ae0706e0afd8784234b0d49d030524c2c0689c20218f735
[root@zfl ~]# docker inspect contacts |egrep -i ipadd
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.5",
                    "IPAddress": "172.17.0.5",
[root@zfl ~]# 

1.2.2 add-list directly creates the container passport and writes it directly into the hosts file of the container

[root@zfl ~]# docker run -itd --name passport  --add-host contacts:172.17.0.5  -p 8009:80 nginx:latest
4999aac0f62d92a3dc8b074f5ab6c36c6f57b0d26552aae7d44f165c4f2fdaf2
[root@zfl ~]# 

[root@zfl ~]# docker exec -it passport bash
root@4999aac0f62d:/# cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.5	contacts
172.17.0.6	4999aac0f62d
root@4999aac0f62d:/# curl contacts
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html {
    
     color-scheme: light dark; }
body {
    
     width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@4999aac0f62d:/# 

2. By link

2.1 First create the called container

[root@zfl ~]# docker run -itd --name contacts -p 800:80 nginx:latest 
84d8e0689e01580567f854a1b863661b867ae16098c1b2344871fc59546327ab
[root@zfl ~]# 

2.2 Use link to call container

[root@zfl ~]# docker run -itd --name passport  --link contacts  -p 8009:80 nginx:latest
df64c51de1530b03b23cef0f6e768c9225de90f2b6f8ea7a6121795c6306f4c9
[root@zfl ~]# 

[root@zfl ~]# docker exec -it passport bash
root@df64c51de153:/# cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.5	contacts 84d8e0689e01
172.17.0.6	df64c51de153
root@df64c51de153:/# 

root@df64c51de153:/# curl contacts
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html {
    
     color-scheme: light dark; }
body {
    
     width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

3. Through the form of custom network

We found that the above two types are one container calling another container. What
can only be realized is that one type calls the other type and cannot access each other.

3.1 Create a docker network

[root@zfl ~]# docker network create --driver bridge --subnet 172.100.0.0/16 --gateway 172.100.0.1 br1  
957f7f074e0a73cce0e509beeb4885597a944f6f1e8876941976e99813acf7a2
55: br-957f7f074e0a: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:c7:2c:36:b6 brd ff:ff:ff:ff:ff:ff
    inet 172.100.0.1/16 brd 172.100.255.255 scope global br-957f7f074e0a
       valid_lft forever preferred_lft forever

[root@zfl ~]# docker network inspect br1
[
    {
    
    
        "Name": "br1",
        "Id": "957f7f074e0a73cce0e509beeb4885597a944f6f1e8876941976e99813acf7a2",
        "Created": "2023-05-07T21:41:03.93632345+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
    
    
            "Driver": "default",
            "Options": {
    
    },
            "Config": [
                {
    
    
                    "Subnet": "172.100.0.0/16",
                    "Gateway": "172.100.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
    
    
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
    
    },
        "Options": {
    
    },
        "Labels": {
    
    }
    }
]
[root@zfl ~]# 

3.2 Create a container using the network card br1

[root@zfl ~]# docker run -itd --name passport --network br1   -p 8009:80 nginx:latest
d4ec6ab13ec3c3685dc10843e46ee97753529a51962ea622f59a262b9cb1fbbe
[root@zfl ~]# docker run -itd --name contacts  --network br1   -p 8010:80 nginx:latest
6320f7352dc734df21995c5ada46c0286b375cebc920089bcb59fa6944e805f0

3.3 Call contacts on passport

[root@zfl ~]# docker exec -it passport bash

root@d4ec6ab13ec3:/# ping contacts
bash: ping: command not found
root@d4ec6ab13ec3:/# curl contacts
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html {
    
     color-scheme: light dark; }
body {
    
     width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

3.4 Call passport in contacts

[root@zfl ~]# docker exec -it contacts bash
root@6320f7352dc7:/# curl passport 
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html {
    
     color-scheme: light dark; }
body {
    
     width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@6320f7352dc7:/# 

3.5 Differences between default network and custom network

Why do you use a custom network in the form of a bridge? Why can’t the docker containers created by default call each other?
Speaking of which, some people may ask, the default network card driver is also in the bridge mode, and the user-defined network is also in the bridge mode. Isn't it just a name change? Why can't the default network card use an alias for IP address resolution?
This question is a good one, and the official explained the difference between the two network cards .

| User-defined bridges provide automatic DNS resolution between containers.

Containers on the default bridge network can only access each other by IP addresses, unless you use the --link option, which is considered legacy. On a user-defined bridge network, containers can resolve each other by name or alias.

The general idea of ​​the translation is that the user-defined network card can provide automatic DNS resolution between containers , and the containers on the default bridge network can only access each other through IP addresses, unless the --link parameter is used. On user-defined NICs, containers can resolve each other directly by name or alias.

Guess you like

Origin blog.csdn.net/xiaolong1155/article/details/130548941