简体   繁体   中英

Unable to connect to redis from docker container

/ Following below snippet is from docker-compose.yml file /

version: "3"
services:
  app:
    container_name: app
    restart: always
    build: .
    ports:
      - "3006:3006"
    # deploy:
    #   mode: replicated
      #replicas: 6
    networks: 
      - overlay
    depends_on:
      - redis
      - mongo
      - mysql
    links:
      - mongo
      - redis
      - mysql
  mongo:
    container_name: mongo
    networks: 
      - overlay
    image: mongo
    volumes:
      - ./data:/data/db
    ports:
      - "27017:27017"
  redis:
    image: redis:latest
    networks: 
      - overlay
    volumes:
      - ./data:/redisdata/redisdb
    ports:
      - "6379:6379"
    logging:
      driver: "json-file"
      options:
        max-size: "100MB"
        max-file: "3"
  mysql:
    image: mysql:latest
    networks: 
     - overlay
    ports: 
    - "3307:3307"
    environment:
      - 'MYSQL_ROOT_PASSWORD=******'
      - 'MYSQL_DATABASE=iconnect_test_demo'
      - 'MYSQL_USER=root'
      - 'MYSQL_PASSWORD=******'
networks:
 overlay:

Upon executing above docker-compose.yml file all the 4 containers are getting created but unable to connect to redis database getting the following exception.Tried replacing redis with localhost but still no luck.But if i run node program independently outside docker its getting connected to redis database which is relying inside the container.

Its resulting to below error during execution.

[ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379
app      |     at Object.exports._errnoException (util.js:1050:11)
app      |     at exports._exceptionWithHostPort (util.js:1073:20)
app      |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1093:14)

Complete Error Log is pasted in below url.
https://pastebin.com/0NvC3nz6

In Node File trying to connect to redis server in the following way.

var Redis = require('ioredis'); 
var redisadapter = require('socket.io-redis');


/* For maintaining connectivity across the multiple nodes */
io.adapter(redisadapter({ host: 'redis', port: 6379 }));
#io.adapter(redisadapter({ host: 'localhost', port: 6379 }));
server.listen(3006);
console.log("server running on 3006");

/* Redis configuration */
var redis_address = 'redis://redis:6379';
#var redis_address = 'redis://localhost:6379';
var redis = new Redis(redis_address);

redis.on('ECONNREFUSED', function(err) {    
  logger.error('Redis error: ' + err);
} );

/ Docker File /

FROM node:7
WORKDIR /app
COPY . /app
CMD node server1.js
EXPOSE 3006

If you look at this specific line in the logs file:

app      | [ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379

It tells you that the app container cannot connect to the redis container. The IP address of the redis instance is 127.0.0.1 on the port 6379. However here, your redis server is not running on the app container but on the redis container. there's something wrong in you code or in the configuration. Check the documentation of the library you use for more details. Also, be sure you rebuild the app docker image each time you change something in your code !

as I read in can't connect to redis through node app, both in dockers

initiating the objects should be done as

const Redis = require('ioredis');
const redis = new Redis({ host: 'redis' });

(remove redis:// prefix and add host: as object key)

if that does not work try add the redis container container name and call it by its name

container_name: svc-redis

and configure the host as svc-redis:6379

谢谢,我在logs.got中解决了它,错过了它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM