【问题标题】:Container won't connect to redis container容器不会连接到 redis 容器
【发布时间】:2019-12-22 05:12:10
【问题描述】:

我正在尝试从运行 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
    })

码头工人撰写

version: "0.1"
services:
  redis_server:
    image: "redis"
    ports:
      - "6379:6379"
  lambda_server:
    build: .
    ports:
      - "8080:50051"
    links:
      - redis_server

【问题讨论】:

  • 与您的问题无关,但您应该在引用 redis_serverlambda_server 中添加 depends_on
  • @Jesse 这与链接有何不同?从措辞我假设如果 redis_server 不运行,它不会允许 lambda_server 运行
  • 啊,没错。我在阅读文档时错过了那篇文章。
  • oftopic:为什么你使用旧版本的撰写语法(版本:“0.1”)?尝试更改为版本:'3.5'

标签: docker go redis


【解决方案1】:

默认情况下,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      [::]:* 

如果这不能解决问题,您可能需要检查任何可能阻止访问的防火墙。

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,这与地址绑定有关。在 redis 配置文件 /etc/redis/redis.conf 中,找到前缀为 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.conf重新启动redis服务器。

    更新

    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。因此,对于您的情况,您可以指定地址或注释该行。

    【讨论】:

    • 我认为这个配置已经在redis镜像中做了。在我的项目中,我使用 redis 映像而不更改配置并从另一个容器连接工作正常
    • @RyabchenkoAlexander 我已经更新了答案,默认地址是 127.0.0.1。
    猜你喜欢
    • 2021-10-31
    • 2018-08-06
    • 2020-11-09
    • 1970-01-01
    • 2022-01-07
    • 2020-06-17
    • 1970-01-01
    • 2023-02-11
    • 2020-02-08
    相关资源
    最近更新 更多