dongle2627
2019-08-15 19:27 阅读 508

容器将无法连接到Redis容器

I'm trying to connect to my redis container from my container running a Go server but the the connection keeps getting refused despite what appears to be a correct setup in my docker-compose.yml:

Go

redisClient = redis.NewClient(&redis.Options{
        Network:  "tcp",
        Addr:     "redis_server:6379",
        Password: "", // no password set
        DB:       0,  // use default DB
    })

docker-compose

version: "0.1"
services:
  redis_server:
    image: "redis"
    ports:
      - "6379:6379"
  lambda_server:
    build: .
    ports:
      - "8080:50051"
    links:
      - redis_server
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongzongzi0379 dongzongzi0379 2019-08-15 19:36

    By default, Redis doesn’t allow remote connections. You can connect to the Redis server only from 127.0.0.1 (localhost) - the machine where Redis is running.

    Replace bind 127.0.0.1 with bind 0.0.0.0 in the /etc/redis/redis.conf file.

    then run sudo service redis-server restart to restart the server.

    Use the following command to verify that redis is listening on all interfaces on port 6379:

    ss -an | grep 6379
    

    You should see something like below. 0.0.0.0 means all IPv4 addresses on the machine.

    tcp  LISTEN 0   128   0.0.0.0:6379   0.0.0.0:*
    tcp  LISTEN 0   128      [::]:6379      [::]:* 
    

    If that doesn't solve the problem, you might need to check any firewalls that might block the access.

    点赞 评论 复制链接分享
  • douji8549 douji8549 2019-08-15 20:27

    I faced similar problem and that was related to address binding. In redis configuration file, /etc/redis/redis.conf, find the line having prefix bind. Usually, this line contains bind 127.0.0.1. This means, only from the same host as the redis server (redis server container in your case) client connections are accepted.

    You need to add the host name or the host ip of your client container in this bind deffinition line, if you want the client connection be accepted.

    bind 127.0.0.1 <client-ip or client-hostname>
    

    Another way to achieve this is binding any address by,

    bind 0.0.0.0
    

    In either case, tou need to restart the redis server with the changed redis.conf.

    Update

    From redis.conf file, we can see the followings:

    # 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
    

    You can see that the bind address is 127.0.0.1 by default. So, for your case, either you can specify the address or comment the line.

    点赞 评论 复制链接分享

相关推荐