我想在 Docker-Swarm 中将 2 个容器相互连接

I want to connect 2 containers with each other in a Docker-Swarm

我想将两个容器相互连接...我从创建覆盖网络开始 mynet:

docker network create -d overlay mynet

之后,我创建了第一个服务 activemq:

docker service create --name activemq -p 61616:61616 -p 8161:8161  --replicas 1 --network mynet rmohr/activemq

启动并运行良好,我也可以访问 WebUI http://localhost:8161/admin/

现在我要启动我的服务TimeService我在容器中有以下设置:

docker service create --name timeservice -p 7000:7000  --replicas 1 --network mynet ni920/timeserviceplain:latest
java.naming.provider.url=tcp://localhost:61616
java.naming.user=admin
java.naming.password=admin
io.jexxa.rest.host=0.0.0.0
io.jexxa.rest.port=7000

所以它应该通过 tcp://localhost:61616 连接到 ActiveMQ 但它没有。

你们知道我应该尝试什么吗,在 none Swarm 环境或 Kubernetes-Pod 环境中,通信完美无缺?

如果您希望您的容器相互通信 ,您可以使用它们的名称然后让网络驱动程序解析它们的 ips。

这是来自 docker 文档的 network driver summary

  • User-defined bridge networks are best when you need multiple containers to communicate on the same Docker host.
  • Host networks are best when the network stack should not be isolated from the Docker host, but you want other aspects of the container to be isolated.
  • Overlay networks are best when you need containers running on different Docker hosts to communicate, or when multiple applications work together using swarm services.
  • Macvlan networks are best when you are migrating from a VM setup or need your containers to look like physical hosts on your network, each with a unique MAC address.
  • Third-party network plugins allow you to integrate Docker with specialized network stacks.

在您的情况下,将 localhost 替换为服务名称 activemq

java.naming.provider.url=tcp://activemq:61616
.
.