docker 容器的互相调用

记录一次数据迁移过程中docker容器的两个应用的调用
在进行数据迁移的过程中,需要创建新的docker容器,但是将创建好容器好,查看日志发现passport容器没有办法调用contacts容器,没有实现容器的调用。(发现他们是通过容器名进行调用的,然后在passport容器ping contacts容器名是不通的)

[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:/# 

记录解决办法

1.通过域名解析的办法(临时办法)

这种办法不适用于以后的扩展

1.1 在passport里面添加contacts的域名解析

1.1.1 查看contacts的ip

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

1.1.2 在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 通过add-list直接创建容器

1.2.1 先创建被调用的容器

[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直接创建容器passport,直接写入容器的hosts文件中

[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.通过link的方式

2.1 先创建被调用的容器

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

2.2 使用link调用容器

[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.通过自定义网络的形式

我们发现上面的两种都是一个容器调用另外一个容器
只能实现的是一种调用另外一种 不能互相访问

3.1 创建一个docker网络

[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 使用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 在passport调用contacts

[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 在contacts调用passport

[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 默认网络和自定义网络区别

为什么用自定义网络 都是桥接的形式 为什么默认创建的docker容器就不能互相调用
说到这里可能有人会问了,那默认的网卡的网卡驱动也是 bridge 模式的,用户自定义的网络也是 bridge 模式,不就是换了一个名字吗,为什么默认的网卡不可以使用别名进行 IP 地址解析呢?
这个问题问得好,官方特意解释了这两个网卡的区别

| 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.

翻译过来大意:就是用户自定义的网卡可以在容器之间提供自动的 DNS 解析,缺省的桥接网络上的容器只能通过 IP 地址互相访问,除非使用 --link 参数。在用户自定义的网卡上,容器直接可以通过名称或者别名相互解析。

猜你喜欢

转载自blog.csdn.net/xiaolong1155/article/details/130548941