gpt4 book ai didi

docker - 使用 docker compose 连接 2 个 docker 容器

转载 作者:行者123 更新时间:2023-12-02 19:08:42 31 4
gpt4 key购买 nike

我有 2 个 docker 容器“restorecms_facade_1”(外观服务)和“restorecms_identity_1”(身份服务)在同一网桥“restorecms_default”上运行。

我已将主机名指定为 'hostname: identity-srv' 用于我的 docker compose 文件中的身份服务镜像

门面服务暴露了 5000 个端口,它接受我的 graphQL 请求。
此外观服务会将请求委托(delegate)给在端口 50051 上运行的身份服务(dns name 'identity-srv'),但该请求当前在外观服务上超时。

我的 docker 容器和新娘网络详细信息如下。

我什至无法从外观服务 ping 到身份服务(但可以反过来)。

我在这里遗漏了什么,还是我需要在外观服务上添加任何东西,以便连接到身份服务?

docker ps:

akumar@client3 /restore $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a8854ce4876 xxx/facade-srv "node lib/index.js" 41 minutes ago Up 41 minutes 0.0.0.0:5000->5000/tcp restorecms_facade_1
00f1f00ae2a6 xxx/identity-srv "node service.js" 41 minutes ago Up 41 minutes (healthy) 0.0.0.0:50051->50051/tcp restorecms_identity_1

docker 检查:
akumar@client3 /restore/identity-srv-TypeScript $ docker inspect restorecms_default 
[
{
"Name": "restorecms_default",
"Id": "102358eab67884f7d39b78fd0bcf1050499d3dc667eddab5e15086633185837d",
"Created": "2017-06-08T10:40:59.672964582+02:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"Containers": {
"00f1f00ae2a64f489a530a63a9fb57711618d5e67769b739db514b5f20b73d36": {
"Name": "restorecms_identity_1",
"EndpointID": "d005cdff65479817bf769e4b60a18769c40d9d9cae396f3c735c2e497d6e08a4",
"MacAddress": "02:42:ac:12:00:09",
"IPv4Address": "172.18.0.9/16",
"IPv6Address": ""
},
"4a8854ce487698149072c224378f697e9309e32649f6010d2d8c4cc4f0bb3f42": {
"Name": "restorecms_facade_1",
"EndpointID": "2509c8e1444d27cbe8a3188412fcbfb1aab103ec2366c22f3ad614c684ef87ab",
"MacAddress": "02:42:ac:12:00:0a",
"IPv4Address": "172.18.0.10/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "default",
"com.docker.compose.project": "restorecms"
}
}
]

主机名和 ping:
root@identity-srv:/# cat /etc/hostname 
identity-srv

root@identity-srv:/# ping 4a8854ce4876
PING 4a8854ce4876 (172.18.0.10): 48 data bytes
56 bytes from 172.18.0.10: icmp_seq=0 ttl=64 time=0.241 ms
56 bytes from 172.18.0.10: icmp_seq=1 ttl=64 time=0.149 ms
56 bytes from 172.18.0.10: icmp_seq=2 ttl=64 time=0.165 ms
^C--- 4a8854ce4876 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.149/0.185/0.241/0.040 ms


root@4a8854ce4876:/# cat /etc/hostname
4a8854ce4876

root@4a8854ce4876:/# ping identity-srv
ping: unknown host

我当前的 docker-compose.yml(用于身份和外观服务)配置如下:
identity:
hostname: identity-srv
image: xxx/identity-srv
ports:
- "50051:50051"
depends_on:
arangodb:
condition: service_healthy
links:
- arangodb
- kafka
healthcheck:
test: "exit 0"

# Facade service
facade:
image: xxx/facade-srv
ports:
- "5000:5000"
depends_on:
identity:
condition: service_healthy
#resource:
# condition: service_healthy
links:
- identity
- kafka
- elasticsearch
- arangodb
- redis

最佳答案

links are legacy .多年来的现代方法一直是确保您的 compose 文件顶部有版本 2(最新的架构版本是 3.6,但在本示例中我将其保留为 2),然后始终保留自定义桥接网络上的所有容器在 DNS 中,并且可以在该网络上相互访问。 Docker 为每个虚拟网络提供了一个私有(private) DNS 服务器,不再使用 hosts用于在同一 docker 网络上解析容器名称的文件。

另请注意,服务名称是它们的默认 DNS 名称,因此最简单的方法是保持它们相同,这样您就不会使用显式的主机名键/值。

这应该有效。您将能够 ping identity来自 facade反之亦然:

version: '2'
services:
identity:
image: xxx/identity-srv
ports:
- "50051:50051"
depends_on:
arangodb:
condition: service_healthy
healthcheck:
test: "exit 0"

facade:
image: xxx/facade-srv
ports:
- "5000:5000"
depends_on:
identity:
condition: service_healthy

关于docker - 使用 docker compose 连接 2 个 docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44432870/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com