search

Home  >  Q&A  >  body text

node.js - Docker 容器里无法连接其他容器运行的 Redis

log

Error: Redis connection to redis:6379 failed - connect ECONNREFUSED 221.179.46.194:6379
    at Object.exports._errnoException (util.js:1022:11)
    at exports._exceptionWithHostPort (util.js:1045:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)

ping

/ # ping mysql
PING mysql (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.101 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.097 ms
64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.100 ms
^C
--- mysql ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.097/0.099/0.101 ms
/ # ping redis
PING redis (172.19.0.3): 56 data bytes
64 bytes from 172.19.0.3: seq=0 ttl=64 time=0.069 ms
64 bytes from 172.19.0.3: seq=1 ttl=64 time=0.134 ms
64 bytes from 172.19.0.3: seq=2 ttl=64 time=0.112 ms
^C
--- redis ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.069/0.105/0.134 ms
/ # 

docker-compose

    redis:
      image: redis
    what:
      image: daocloud.io/who/what
      restart: always
      depends_on:
        - mysql
        - redis
黄舟黄舟2907 days ago771

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-17 16:20:12

    How are the commands and ports mapped when running the container?

    reply
    0
  • PHPz

    PHPz2017-04-17 16:20:12

    Found the reason

    1. The hostname has been changed here and needs to be injected separately https://github.com/mcollina/m...
      server.jsserver.js

    原本的写法

    persistence: {
      factory: mosca.persistence.Redis,
      url: config.redis.host.concat(':').concat(config.redis.port) // 拼接出来不合法的参数redis:6379
    }

    更正

    persistence: {
      factory: mosca.persistence.Redis,
      host: 'your redis host',
      port: 'your redis port'
    }
    1. persistance的构造方法
      lib/persistence/redis.js

    Original writing
      var Redis = require("ioredis");
      RedisPersistence.prototype._buildClient = function() {
        var options = this.options.redisOptions || {};
      
        if (this.options.host) {
          options.host = this.options.host;
        }
      
        if (this.options.port) {
          options.port = this.options.port;
        }
      
        if (this.options.db) {
          options.db = this.options.db;
        }
      
        if (this.options.password) {
          options.password = this.options.password;
        }
      
        return new Redis(options);
      };
    1. Correction

      var redisOnPort6380 = new Redis(6380);
      var anotherRedis = new Redis(6380, '192.168.100.1');
      var unixSocketRedis = new Redis({ path: '/tmp/echo.sock' });
      var unixSocketRedis2 = new Redis('/tmp/echo.sock');
      var urlRedis = new Redis('redis://user:password@redis-service.com:6379/');
      var urlRedis2 = new Redis('//localhost:6379');
      var authedRedis = new Redis(6380, '192.168.100.1', { password: 'password' });

    2. Construction method of persistence
    lib/persistence/redis.js

    rrreee

    This is an ioredis API, refer to https://github.com/luin/iored...🎜Only supports the following parameters, and does not support url parameters🎜🎜🎜 rrreee 🎜🎜🎜I just don’t know why no error is reported when there is a connection error? ? ? Keep trying again until the memory explodes🎜🎜. 🎜

    reply
    0
  • Cancelreply