且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

错误:Redis 连接到 127.0.0.1:6379 失败 - 连接 ECONNREFUSED 127.0.0.1:6379

更新时间:2022-11-16 23:26:01

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