asp.net - swarm 中的 Docker 容器无法看到彼此

标签 asp.net docker asp.net-core redis docker-swarm

我刚刚开始学习docker。我正在运行一个 swarm,并且正在尝试让我的 asp.net core 应用程序与 redis 服务器进行通信。我没想到我会遇到这么多问题:(

我的docker-compose:

version: ‘3.3’

services:
   webfarm_api:
     image: dispersia/webfarm.api
     ports:
       - 8080:80
     environment:
       - SERVICE_PORTS=8080
     depends_on:
       - webfarm_redis

  webfarm_redis:
    image: dispersia/redis
    ports:
      - 6379:6379
    volumes:
      - ‘redis:/data’

volumes:
  redis:

我已将 redis.conf 设置为绑定(bind)到 0.0.0.0,并且在我的应用程序中我有

services.AddDistributedRedisCache(options =>
{
    options.Configuration = “0.0.0.0”;
    options.InstanceName = “redisInstance”;
});

如果我在 docker 之外手动运行 redis-server 和我的 asp.net core 应用程序,它们会互相看到并且一切正常,但我一生都无法让它在 docker 内部工作。

如果我运行 docker检查网络 prod_default 我发现两个容器都在运行,并且它们确实有不同的IP(10.0.0.6/24)和(10.0.0.4/24)但是我认为0.0.0.0将允许任何容器在同一网络内查看它。如果我的假设是错误的,请告诉我。

这是使这些容器通信的正确方法吗?

谢谢

最佳答案

127.0.0.1 与 0.0.0.0 的描述取自 https://superuser.com/questions/949428/whats-the-difference-between-127-0-0-1-and-0-0-0-0

127.0.0.1和0.0.0.0有什么区别?

127.0.0.1 是环回地址(也称为 localhost)。

0.0.0.0 是不可路由的元地址,用于指定无效、未知或不适用的目标(无特定地址占位符)。

在路由条目的上下文中,它通常表示默认路由。

In the context of servers, 0.0.0.0 means "all IPv4 addresses on the local machine". If a host has two IP addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs.

那么这是什么意思呢?这意味着在隔离的应用程序容器中,您告诉应用程序在本地主机中查找此端口的 Redis 实例,但事实并非如此,因为 Redis 正在运行一个单独的隔离 Redis 容器。

您想要做的是在应用程序中使用您的 webfarm_redis 名称,该名称将询问 Docker DNS 服务 webfarm_redis 在哪里,它会给您 docker 的名称Redis IP。

services.AddDistributedRedisCache(options =>
{
    options.Configuration = “webfarm_redis”;
    options.InstanceName = “redisInstance”;
});

我会将 webfarm_redis 作为环境变量传递,以便无需重新构建即可更改它。

关于asp.net - swarm 中的 Docker 容器无法看到彼此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48309079/

相关文章:

ASP.NET 自定义验证器错误消息 : Control referenced by the property cannot be validated

docker - Dockerfile找不到复制的文件夹

c# - 在 .NET Core 测试项目中构建时禁用/阻止 XUnit 警告

asp.net-core - 在 .NET 中配置后如何访问 AWS secret ?

c# - 如何用 RadWindow 替换警告消息框

asp.net - 在 asp.net mvc 应用程序中处理未处理异常的一些策略是什么?

docker - 将 Docker 父主机 ip 插入到容器的 hosts 文件中

docker - 无法让 PyC​​harm 在 docker 上使用远程解释器

c# - ASP.NET Core 中的 Request.InputStream

ios - 为什么我没有收到 eBay 的产品价格回复?