我有一个Express应用程序和React应用程序,在后端部分,我正在使用Redis。我为前端设置了一个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);
});
};
配置在哪里读取我的.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连接?
答案 0 :(得分:3)
从日志看来,它似乎尝试连接到localhost
(127.0.0.1
)上的REDIS。 express docker容器可以通过服务名称redis
到达REDIS。
尝试将localhost
中的redis
替换为redisConnectionString
。像这样:
redis://[[user][:password@]]redis:6379
希望能解决您的问题。