错误:Redis 连接到 127.0.0.1:6379 失败 - 连接 ECONNREFUSED 127.0.0.1:6379 [英] Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379

查看:406
本文介绍了错误:Redis 连接到 127.0.0.1:6379 失败 - 连接 ECONNREFUSED 127.0.0.1:6379的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试允许我的 nodeJs docker 镜像与我的 redis docker 镜像(Mac OS X 环境)之间进行通信:

I'm trying to allow communication between my nodeJs docker image with my redis docker image (Mac OS X environment):

nodeJs Dockerfile:

nodeJs Dockerfile:

FROM node:4.7.0-slim
EXPOSE 8100
COPY . /nodeExpressDB
CMD ["node", "nodeExpressDB/bin/www"]

redis Dockerfile:

redis Dockerfile:

FROM ubuntu:14.04.3
EXPOSE 6379
RUN apt-get update && apt-get install -y redis-server

尝试连接redis的nodeJs代码是:

nodeJs code which is trying to connect to redis is:

var redis = require('redis');
var client = redis.createClient();

docker 构建步骤:

docker build steps:

docker build -t redis-docker .
docker build -t node-docker .

docker run 镜像步骤流程:

docker run images steps flow:

docker run -p 6379:6379 redis-docker
docker run -p 8100:8100 node-docker

错误:

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
    at Object.exports._errnoException (util.js:907:11)
    at exports._exceptionWithHostPort (util.js:930:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1078:14)

我应该怎么做才能从 node-docker 连接到 Redis?

What should I do inorder to connect to Redis from node-docker?

推荐答案

Redis 在一个单独的容器中运行,该容器具有单独的虚拟以太网适配器和 IP 地址到您的节点应用程序正在运行的容器中.您需要 link 两个容器或创建一个用户定义的网络为他们

Redis runs in a seperate container which has seperate virtual ethernet adapter and IP address to the container your node application is running in. You need to link the two containers or create a user defined network for them

docker network create redis
docker run -d --net "redis" --name redis redis
docker run -d -p 8100:8100 --net "redis" --name node redis-node

然后在 node 中连接时指定主机 redis 以便 redis 客户端尝试连接到 redis 容器而不是默认的 localhost

Then specify the host redis when connecting in node so the redis client attempts to connect to the redis container rather than the default of localhost

const redis = require('redis')
const client = redis.createClient(6379, 'redis')
client.on('connect', () => console.log('Connected to Redis') )

Docker Compose 可以帮助定义多容器设置.

Docker Compose can help with the definition of multi container setups.

version: '2'
services:
  node:
    build: .
    ports:
    - "8100:8100"
    networks:
    - redis
  redis:
    image: redis
    networks:
    - redis
networks:
  redis:
    driver: bridge

这篇关于错误:Redis 连接到 127.0.0.1:6379 失败 - 连接 ECONNREFUSED 127.0.0.1:6379的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆