我使用docker版本18.06.1-ce并编写版本1.22.0。
根据docker,应该可以使用服务名称来调用服务。这对我来说很有用,没有swarm模式的docker compose,但是在swarm模式下它不起作用。我甚至尝试在我的作品中设置别名但没有结果。
下面是我的docker-compose.yml
version: "3"
networks:
my_network:
external:
name: new_network
services:
config-service:
image: com.test/config-service:0.0.1
deploy:
placement:
constraints: [node.role == manager]
resources:
limits:
memory: 1024M
reservations:
memory: 768M
restart_policy:
condition: on-failure
healthcheck:
test: ["CMD", "curl", "-f", "http://config-service:8888/health"]
interval: 5s
timeout: 3s
retries: 5
ports:
- 8888:8888
networks:
my_network:
aliases:
- config-service
eureka-service:
image: com.test/eureka-service:0.0.1
deploy:
placement:
constraints: [node.role == manager]
resources:
limits:
memory: 1536M
reservations:
memory: 1024M
restart_policy:
condition: on-failure
healthcheck:
test: ["CMD", "curl", "-I", "http://eureka-service:8761/health"]
interval: 5s
timeout: 3s
retries: 5
ports:
- 8761:8761
depends_on:
- config-service
networks:
my_network:
aliases:
- eureka-service
当我检查我的网络时,我发现了
[
{
"Name": "new_network",
"Id": "s2m7yq7tz4996w7eg229l59nf",
"Created": "2018-08-30T13:58:59.75070753Z",
"Scope": "swarm",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.20.0.0/16",
"Gateway": "172.20.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"355efe27067ee20868455dabbedd859b354d50fb957dcef4262eac6f25d10686": {
"Name": "test_eureka-service.1.a4pjb3ntez9ly5zhu020h0tva",
"EndpointID": "50998abdb4cd2cd2f747fadd82be495150919531b81a3d6fb07251a940ef2749",
"MacAddress": "02:42:ac:14:00:02",
"IPv4Address": "172.20.0.2/16",
"IPv6Address": ""
},
"5cdb398c598c1cea6b9032d4c696fd1581e88f0644896edd958ef59895b698a4": {
"Name": "test_config-service.1.se8ajr73ajnjhvxt3rq31xzlm",
"EndpointID": "5b3c41a8df0054e1c115d93c32ca52220e2934b6f763f588452c38e60c067054",
"MacAddress": "02:42:ac:14:00:03",
"IPv4Address": "172.20.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
现在,如果我连接到容器终端并使用长名称'test_config-service.1.se8ajr73ajnjhvxt3rq31xzlm'进行ping操作,则可以ping通但不能'config-service'。
我相信您遇到的问题是因为您使用的是swarm范围的桥接网络,而不是覆盖网络。我不确定是否支持此配置。在群集模式下部署时,服务的DNS条目位于服务级别,而不是单个容器。根据我的测试,该DNS条目以及设置VIP的代码似乎只适用于覆盖网络。如果您确实需要将网络配置为桥接,则可能需要遵循此问题:https://github.com/moby/moby/issues/37672
否则,最简单的解决方法是用覆盖网络替换您的网络。您可以删除网络别名,因为它们是冗余的。如果主机上还有其他容器也需要在此网络上,则从群集模式外部,请务必将覆盖网络配置为“可连接”。如果您当前有其他应用程序连接到网络,您可以将其替换为新网络,或者如果您需要保留相同的网络名称,请分两个阶段进行交换:
# create a temporary network to free up the new_network name
docker network create -d overlay --attachable temp_network
docker network connect temp_network $container_id # repeat for each container
# finish the above step for all containers before continuing
docker network disconnect new_network $container_id #repeat for each container
# remove the old bridge network
docker network rm new_network
# now create a new_network as overlay
docker network create -d overlay --attachable new_network
docker network connect new_network $container_id # repeat for each container
# finish the above step for all containers before continuing
docker network disconnect temp_network $container_id #repeat for each container
# cleanup the temporary network
docker network rm temp_network
如果一切都在群模式下运行,那么就不需要--attachable
了。之后,您应该能够启动您的swarm模式堆栈。
尝试使用docker service ls
命令列出您的服务。因为如果使用堆栈并为堆栈命名,则服务名称将为nameofstack_config-service
我在你的检查test_eureka-service.1xxxxxx中看到,所以服务名称应该是test_eureka-service
这是版本18.06的已知问题:
https://github.com/docker/for-win/issues/2327
https://github.com/docker/for-linux/issues/375
试试18.03