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

使用Docker Compose连接本地Redis至容器失败,求助排查

解决Docker Compose添加Redis后出现的exec错误

看起来你的问题并不是Redis服务本身的配置问题,而是Docker Compose在解析配置或复用缓存时出现了混淆,导致错误地把容器ID片段当成了要执行的命令。让我们一步步解决这个问题:

第一步:清理旧容器与缓存

错误信息里的3ed84a23c52d是某个容器的ID片段,大概率是Docker Compose复用了旧的容器缓存导致的解析异常。先彻底清理相关资源:

  • 停止并删除所有相关服务、容器和卷:
    docker-compose down -v
    
  • (可选但推荐)清理Docker中无用的镜像、容器和缓存:
    docker system prune -f
    

第二步:优化你的docker-compose.yml配置

你当前使用的links指令在Compose v3版本中已经是过时用法,Compose会自动为所有服务创建默认网络,服务之间可以直接通过服务名(比如redis)互相访问,不需要手动添加links。修改后的配置如下:

version: "3"
services:
  test:
    build: .
    stdin_open: true
    tty: true
    command: nodemon --delay 6 index.js
    volumes:
      - .:/opt/test
    ports:
      - "5007:5007"
    # 移除过时的links配置
  redis:
    image: redis:latest
    container_name: qbo_redis
    restart: always
    ports:
      - "6379:6379"

第三步:重新构建并启动服务

为了确保不使用旧的构建缓存,先重新构建镜像:

docker-compose build --no-cache

然后启动服务:

docker-compose up

额外注意事项

你的Node.js应用连接Redis时,不要用localhost:6379,而是要用服务名redis作为主机地址,比如:

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

因为在Docker Compose的默认网络里,redis会被解析为Redis容器的IP地址,这样容器间就能正常通信了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 10:59:07