You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过Node.js连接Redis/Redis-Stack:问题排查求助

问题描述

按照Redis-Stack的Docker文档尝试用Node.js连接Redis,但Redis容器日志持续出现以下警告:

redis-stack-1 | 9:M 09 Jun 2024 03:12:50.151 * <redisgears_2> Failed loading RedisAI API.
redis-stack-1 | 9:M 09 Jun 2024 03:12:50.151 * <redisgears_2> RedisGears v2.0.20, sha='9b737886bf825fe29ddc2f8da81f73cbe0b4e858', build_type='release', built_for='Linux-ubuntu22.04.aarch64', redis_version:'7.2.4', enterprise:'false'.

文件夹结构如下:
文件夹结构截图

Dockerfile内容

# Use the official Node.js image from the Docker Hub
FROM node:14
# Create and set the working directory inside the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json files
COPY package*.json ./
# Install the dependencies
RUN npm install
# Expose the port the app runs on
EXPOSE 3000
# Start the application
CMD ["npm", "run", "dev"]

docker-compose.yml内容

version: "3.6"
services:
  my-node-app:
    image: my-node-app
    build: .
    ports:
      - "3000:3000"
    volumes:
      - .:/usr/src/app
  redis-stack:
    image: redis/redis-stack:latest
    environment:
      - REDIS_ARGS="--requirepass mypassword"
    ports:
      - 6379:6379
      - 8001:8001
    healthcheck:
      test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
    volumes:
      - redis_data:/data
volumes:
  redis_data:

index.js内容

const express = require("express");
const redis = require("redis");
const app = express();
const port = 3000;

// Create Redis client with Docker Compose service name as hostname
const client = redis.createClient({
  host: "redis-stack", // Use the service name from Docker Compose
  port: 6379,
  password: "mypassword", // Change this to your actual Redis password
});

client.on("connect", () => {
  console.log("Connected to Redis");

  // Example: Insert data into Redis after successful connection
  client.set("mykey", "Hello Redis!", (err, reply) => {
    if (err) {
      console.error("Error setting key in Redis:", err);
    } else {
      console.log("Key set successfully in Redis:", reply);
    }
  });
});

client.on("error", (err) => {
  console.error("Error connecting to Redis:", err);
});

app.get("/", (req, res) => {
  res.send("Hello Docker Compose!");
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

package.json内容

{
  "name": "redis-node",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "devDependencies": {
    "nodemon": "^3.1.3"
  },
  "dependencies": {
    "express": "^4.19.2",
    "redis": "^4.6.14"
  }
}

请问哪里配置出错了?


问题分析与解决

1. RedisAI加载警告处理

这个警告是因为redis/redis-stack:latest镜像在你的ARM64架构下未预装RedisAI组件,RedisGears加载相关API失败。这只是警告,不影响Redis核心功能和Node.js连接。如果不需要RedisAI、RedisGears这类扩展组件,可以改用redis/redis-stack-server:latest镜像,该镜像仅包含Redis核心和RedisInsight,不会出现该警告。

2. Node.js Redis客户端配置修正

你使用的是Redis客户端v4版本,但配置写法还是v3的格式,且缺少主动连接步骤:

  • v4版本需调整客户端配置格式:
// 方式一:使用socket和password分开配置
const client = redis.createClient({
  socket: {
    host: "redis-stack",
    port: 6379
  },
  password: "mypassword"
});

// 方式二:使用URL字符串配置
const client = redis.createClient({
  url: "redis://:mypassword@redis-stack:6379"
});
  • v4版本需要手动调用连接方法,否则不会触发connect事件,需添加:
client.connect().catch(err => console.error(err));

3. Docker Compose环境变量修正

REDIS_ARGS的引号会被当作参数的一部分,导致Redis无法正确识别密码,需去掉引号:

environment:
  - REDIS_ARGS=--requirepass mypassword

4. 健康检查命令修正

因为设置了Redis密码,默认健康检查命令会因无权限执行失败,需修改为:

healthcheck:
  test: ["CMD", "redis-cli", "-a", "mypassword", "--raw", "incr", "ping"]

内容的提问来源于stack exchange,提问作者desh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 17:44:51