Py学习  »  Redis

使用docker compose设置redis节点

terett • 4 年前 • 950 次点击  

我有一个快速应用和反应应用,在后端部分,我使用的是Redis。我为前端设置了一个Dockerfile,为后端设置了一个Dockerfile。另外,我设置了 docker-compose.yml 文件,如下所示:

# Specify docker-compose version.
version: '3'

# Define the services/containers to be run.
services:
  react:
    build: admin
    ports:
      - '3000:3000'

  express:
    build: .
    container_name: api
    ports:
      - '3001:3001'
    depends_on:
      - redis
    links:
      - mongo
      - redis
    environment:
      - REDIS_URL=redis://cache
      - MONGO_URL=mongodb://db/tests

  mongo:
    image: mongo:4
    container_name: db
    ports:
      - '27017:27017'

  redis:
    image: redis:4
    container_name: cache
    ports:
      - '6379:6379'

在我的后台,我打电话给 redisClient 具体如下:

const bluebird = require('bluebird');
const config   = require('config');
const logger   = require('./logger');
const redis    = require('redis');
bluebird.promisifyAll(redis);

const RedisService = function() {
    const redisConnectionString = process.env.REDIS_URL;
    this.client = redis.createClient({ url: redisConnectionString });
    this.client.on('error', (err) => {
        logger.error(err);
    });
};

其中config读取 .json 我的文件 config 文件夹。但是,当我跑的时候 docker-compose up ,将引发以下错误:

express_1  | [2019-06-10T20:14:38.169Z] error: "uncaughtException: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14) 

在我的设置中,我从config.json文件中读取连接字符串,有什么想法可以正确连接Redis和docker compose吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50789
 
950 次点击  
文章 [ 1 ]  |  最新文章 4 年前
b0gusb
Reply   •   1 楼
b0gusb    4 年前

从日志来看,它似乎试图连接到 localhost ( 127.0.0.1 ). express docker容器可以通过服务名到达REDIS,服务名是 redis .

尝试替换 本地服务器 具有 redis公司 在里面 redisConnectionString . 类似于:

redis://[[user][:password@]]redis:6379

希望这能解决你的问题。