我刚刚开始学习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
and10.1.2.1
, and a server running on the host listens on0.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/