错误:Redis与0.0.0.0:6379的连接失败-连接ECONNREFUSED 0.0.0.0:6379

时间:2020-10-08 21:15:15

标签: docker redis docker-compose node-redis

我一直在尝试构建经典的投票应用程序。但是我的redis和mongodb给出了相同的错误。我必须将Redis连接到我的节点应用程序,并且尝试了以下操作:

//redis
const redisoption = {
    host: "redis",
    port: 6379
};

//redis
const redisoption = {
    host: "0.0.0.0",
    port: 6379
};

docker-compose.yml

version: "3"

服务:

vote:
    image: adityagaddhyan/voting-app
    ports: 
        - "3000:3000"
    depends_on:
        - redis
    networks:
        - front-end
        - back-end
    volumes: 
        - ./vote:/app
result:
    image: adityagaddhyan/voting-result
    ports: 
        - "3011:3011"
    depends_on:
        - mongodb
    networks: 
        - back-end
        - front-end
    volumes: 
        - ./result:/app

worker:
    image: adityagaddhyan/service-worker
    depends_on: 
        - redis
        - mongodb
    networks: 
        - back-end
    volumes: 
        - ./worker:/app
redis:
    image : redis
    command: ["redis-server", "--bind", "0.0.0.0", "--port", "6379"
    ports: 
        - "6379:6379"
    networks: 
        - back-end
    volumes: 
        - ./redis:/app
mongodb:
    image: mongo
    ports:
        - "27017:27017"
    volumes: 
        - ./db:/app

卷: db-data:

网络: 前端: 后端:

我尝试了各种组合。 我主要遇到两种错误。 如果我使用host: "0.0.0.0",我会得到

    Error: Redis connection to 0.0.0.0:6379 failed - connect ECONNREFUSED 0.0.0.0:6379
vote_1     |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
vote_1     | Emitted 'error' event on RedisClient instance at:
vote_1     |     at RedisClient.on_error (/usr/src/app/node_modules/redis/index.js:341:14)
vote_1     |     at Socket.<anonymous> (/usr/src/app/node_modules/redis/index.js:222:14)
vote_1     |     at Socket.emit (events.js:315:20)
vote_1     |     at emitErrorNT (internal/streams/destroy.js:92:8)
vote_1     |     at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
vote_1     |     at processTicksAndRejections (internal/process/task_queues.js:84:21) {
vote_1     |   errno: 'ECONNREFUSED',
vote_1     |   code: 'ECONNREFUSED',
vote_1     |   syscall: 'connect',
vote_1     |   address: '0.0.0.0',
vote_1     |   port: 6379
vote_1     | }

如果我使用host:redis,我会得到:

    Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
worker_1   |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
worker_1   | Emitted 'error' event on RedisClient instance at:
worker_1   |     at RedisClient.on_error (/usr/src/app2/node_modules/redis/index.js:341:14)
worker_1   |     at Socket.<anonymous> (/usr/src/app2/node_modules/redis/index.js:222:14)
worker_1   |     at Socket.emit (events.js:315:20)
worker_1   |     at emitErrorNT (internal/streams/destroy.js:92:8)
worker_1   |     at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
worker_1   |     at processTicksAndRejections (internal/process/task_queues.js:84:21) {
worker_1   |   errno: 'ECONNREFUSED',
worker_1   |   code: 'ECONNREFUSED',
worker_1   |   syscall: 'connect',
worker_1   |   address: '127.0.0.1',
worker_1   |   port: 6379
worker_1   | }
vote_1     | /usr/src/app/app.js:28
vote_1     |     host: back-end,
vote_1     |           ^

我是docker的新手,无法解决此问题。请帮忙。

0 个答案:

没有答案