在docker中连接Node.js到Redis“错误:客户端已关闭”

b91juud3  于 12个月前  发布在  Redis
关注(0)|答案(1)|浏览(170)

docker-compose.yml有这样的东西:

version : "3"
services:
  irbredis:
    image: redis:7.2.2-alpine
    container_name: irbredis
  irbserver:
    image: node:20.9.0-alpine
    container_name: irbserver
    depends_on:
      - irbredis

字符串
在node.js容器的某个地方,我得到了这个:

const redis = require('redis');
const rclient = redis.createClient({ url: "redis://irbredis:6379" });


redis服务器肯定是运行的,我可以从docker-compose中暴露端口,然后去URL检查日志。但是从node.js代码中,我得到了错误日志:

irbserver  | Error: The client is closed
irbserver  |     at Commander._RedisClient_sendCommand (/app/node_modules/@redis/client/dist/lib/client/index.js:494:31)
irbserver  |     at Commander.commandsExecutor (/app/node_modules/@redis/client/dist/lib/client/index.js:189:154)
irbserver  |     at BaseClass.<computed> [as get] (/app/node_modules/@redis/client/dist/lib/commander.js:8:29)
irbserver  |     at Object.get (/app/node_modules/connect-redis/dist/cjs/index.js:20:34)
irbserver  |     at RedisStore.get (/app/node_modules/connect-redis/dist/cjs/index.js:53:42)
irbserver  |     at session (/app/node_modules/express-session/index.js:485:11)
irbserver  |     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
irbserver  |     at trim_prefix (/app/node_modules/express/lib/router/index.js:328:13)
irbserver  |     at /app/node_modules/express/lib/router/index.js:286:9
irbserver  |     at Function.process_params (/app/node_modules/express/lib/router/index.js:346:12)


我做错了什么?
更多context/node.js代码:
redisFunc.js

const redis = require('redis');
const rclient = redis.createClient({ url: "redis://irbredis:6379" });

module.exports =
{
    rclient : rclient
}


sessionMW.js中使用:

const session    = require('express-session');
const RedisStore = require('connect-redis').default;

module.exports =
session
(
    {
        store: new RedisStore({ client : require('./redisFunc.js').rclient }),
        //...
    }
);


index.js中使用

const express = require('express');
const app     = express();   

app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.use(require('./sessionMW.js'));
//...


版本:

"connect-redis": "^7.1.0",
"express": "^4.18.2",
"express-session": "^1.17.3",
"redis": "^4.6.10",

nkoocmlb

nkoocmlb1#

使用docker-compose网络(或一般的docker),您需要做的第一件事通常是公开必要的容器端口。在这种情况下,您希望默认网络上的容器能够访问irbredis容器上的端口6379,因此您需要(至少)添加以下内容

ports:
      - "6379"

字符串
irbredis服务的配置。
在这一点上,如果你仍然得到连接关闭的问题,(并且您可以验证网络是否已启动并可从应用程序容器中访问,这通常涉及在该容器中运行交互式shell并向该端口发送ping),接下来要查看的是应用程序代码中的错误(例如忘记连接,或在准备好之前尝试使用连接)。

相关问题