错误:REDIS连接到127.0.0.1:6379失败 - 连接Econnrefuse 127.0.0.1:6379

发布于 2025-02-10 07:16:03 字数 970 浏览 4 评论 0原文

正在尝试通过我的docker image(Mac OS X环境)之间的Nodejs Docker

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

之间

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

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

映像

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

通信:

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?

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

nodeJs Dockerfile:

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

redis Dockerfile:

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

nodeJs code which is trying to connect to redis is:

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

docker build steps:

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

docker run images steps flow:

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

ERROR:

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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不喜欢何必死缠烂打 2025-02-17 07:16:04
  1. 下载Redis服务器。
  2. 运行REDIS服务器。
  3. 然后运行您的项目。

它应该很好。这是下载链接:

github -redis download download package

我希望它可以正常工作。

  1. Download the redis server.
  2. run the redis server.
  3. and then run your project.

It should work just fine. Here's the download link:

Github - Redis Download Packages

I hope it works.

醉生梦死 2025-02-17 07:16:03

REDIS在一个单独的容器中运行,该容器具有单独的虚拟以太网适配器和IP地址到您的节点应用程序正在运行的容器中。 /links“ rel =“ noreferrer”>链接两个容器或创建用户定义的网络为他们

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

指定主机redis在节点中连接时,Redis客户端尝试连接到redis> redis容器而不是容器 值

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

localhost docker compose 默认 容器设置。

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

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

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 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
天冷不及心凉 2025-02-17 07:16:03

解决方案

const client = redis.createClient({
    host: 'redis-server',
    port: 6379
})

然后用=&gt重建Ur Docker; docker-compoped-build

Solution

const client = redis.createClient({
    host: 'redis-server',
    port: 6379
})

Then rebuild ur docker with => docker-compose up --build

↘紸啶 2025-02-17 07:16:03

在节点中连接时,在函数参数中使用redis-docker您传递了服务器IP。

When connecting in node, use redis-docker in the function argument you pass the server IP.

老子叫无熙 2025-02-17 07:16:03

如果已安装了redis,则运行命令

sudo apt-get install install redis-server

,您将运行您的站点。

if redis installed then run command

sudo apt-get install redis-server

Then you will get your site running.

流年里的时光 2025-02-17 07:16:03

如果您可以创建一个新的Redis Docker实例,请尝试映射容器端口以进行主机:

docker run --name some-redis -p 6379:6379 -d redis

docker container start some-redis

现在,您可以启动容器并与主机127.0.0.1连接

const redis = require("redis");
const client = redis.createClient({
    host: '127.0.0.1',
    port: '6379'
});

If you can create a new redis docker instance, try mapping the container port to host:

docker run --name some-redis -p 6379:6379 -d redis

docker container start some-redis

Now, you can start the container and connect with host 127.0.0.1

const redis = require("redis");
const client = redis.createClient({
    host: '127.0.0.1',
    port: '6379'
});
沐歌 2025-02-17 07:16:03

您还可以更改 /etc /hosts文件以更新Redis容器的DockErip。

通过使用Docker Inspect查找Docker IP

You can also change your /etc/hosts file to update the dockerip of the redis container.

Find the docker ip by using docker inspect

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文