容器无法连接到 redis 容器

我正在尝试从运行 Go 服务器的容器连接到我的 redis 容器,但连接不断被拒绝,尽管我的 docker-compose.yml 中的设置似乎是正确的:


redisClient = redis.NewClient(&redis.Options{

        Network:  "tcp",

        Addr:     "redis_server:6379",

        Password: "", // no password set

        DB:       0,  // use default DB

    })

docker 撰写

version: "0.1"

services:

  redis_server:

    image: "redis"

    ports:

      - "6379:6379"

  lambda_server:

    build: .

    ports:

      - "8080:50051"

    links:

      - redis_server


小唯快跑啊
浏览 147回答 2
2回答

慕哥6287543

默认情况下,Redis 不允许远程连接。您只能从 127.0.0.1 (localhost)(运行 Redis 的计算机)连接到 Redis 服务器。替换/etc/redis/redis.conf 文件中bind 127.0.0.1的。bind 0.0.0.0然后运行sudo service redis-server restart以重新启动服务器。使用以下命令验证 redis 是否正在侦听端口 6379 上的所有接口:ss -an | grep 6379您应该看到如下所示的内容。0.0.0.0 表示计算机上的所有 IPv4 地址。tcp  LISTEN 0   128   0.0.0.0:6379   0.0.0.0:* tcp  LISTEN 0   128      [::]:6379      [::]:*如果这不能解决问题,您可能需要检查可能阻止访问的防火墙。

慕婉清6462132

我遇到了类似的问题,这与地址绑定有关。在 redis 配置文件 /etc/redis/redis.conf 中,找到具有 prefix 的行bind。通常,该行包含bind 127.0.0.1. 这意味着,仅接受来自与 redis 服务器(在您的情况下是 redis 服务器容器)相同的主机的客户端连接。如果您希望接受客户端连接,则需要在此绑定定义行中添加客户端容器的主机名或主机 IP。bind 127.0.0.1 <client-ip or client-hostname>实现此目的的另一种方法是绑定任何地址,bind 0.0.0.0无论哪种情况,都需要使用更改后的 .redis 文件重新启动 redis 服务器redis.conf。更新从redis.conf文件中,我们可以看到以下内容:# By default, if no "bind" configuration directive is specified, Redis listens# for connections from all the network interfaces available on the server.# It is possible to listen to just one or multiple selected interfaces using# the "bind" configuration directive, followed by one or more IP addresses.## Examples:## bind 192.168.1.100 10.0.0.1# bind 127.0.0.1 ::1## ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the# internet, binding to all the interfaces is dangerous and will expose the# instance to everybody on the internet. So by default we uncomment the# following bind directive, that will force Redis to listen only into# the IPv4 loopback interface address (this means Redis will be able to# accept connections only from clients running into the same computer it# is running).## IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES# JUST COMMENT THE FOLLOWING LINE.bind 127.0.0.1可以看到默认的绑定地址是127.0.0.1。因此,对于您的情况,您可以指定地址或注释该行。
打开App,查看更多内容
随时随地看视频慕课网APP