我正在尝试开发一个expressapi,它可以在本地机器上正常工作。我用的是docker,但在docker和heroku redis的生产上没有用
dockerfile文件
FROM node:latest
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 5000
CMD ["npm","start"]
docker.compose.yml文件
version: '3'
services:
mongo:
container_name: mongo
image: mongo
ports:
- '27017:27017'
redis:
container_name: redis
image: redis
app:
container_name: password-manager-docker
image: app
restart: always
build: .
ports:
- '80:5000'
links:
- mongo
- redis
environment:
MONGODB_URI: ${MONGODB_URI}
REDIS_URL: ${REDIS_URL}
clientID: ${clientID}
clientSecret : ${clientSecret}
PORT: ${PORT}
REDIS_HOST: ${REDIS_HOST}
JWT_SECRET_KEY: ${JWT_SECRET_KEY}
JWT_EXPIRE: ${JWT_EXPIRE}
REFRESH_TOKEN: ${REFRESH_TOKEN}
JWT_REFRESH_SECRET_KEY: ${JWT_REFRESH_SECRET_KEY}
JWT_REFRESH_EXPIRE: ${JWT_REFRESH_EXPIRE}
JWT_COOKIE: ${JWT_COOKIE}
SMTP_HOST: ${SMTP_HOST}
SMTP_PORT: ${SMTP_PORT}
SMTP_USER: ${SMTP_USER}
SMTP_PASS: ${SMTP_PASS}
redis文件
const asyncRedis = require('async-redis');
//process.env.REDIS_HOST's value is redis
const redisClient = asyncRedis.createClient({port:6379,host:process.env.REDIS_HOST || "127.0.0.1"});
redisClient.on("connect",() => {
console.log(`Redis: ${host}:${port}`);
})
redisClient.on('error', function(err) {
// eslint-disable-next-line no-console
console.log(`[Redis] Error ${err}`);
});
heroku上的错误是“[redis]error error:redis connection to 127.0.0.1:6379 failed-connect econnrefered 127.0.0.1:6379”。它在没有 Docker 的情况下在heroku工作,但它不工作。谢谢你的帮助
3条答案
按热度按时间n3h0vuf21#
环境变量
REDIS_HOST
未设置-您的问题中已有答案,错误为:ECONNREFUSED 127.0.0.1:6379
主机地址来自您的if语句:{port:6379,host:process.env.REDIS_HOST || "127.0.0.1"}
如果未设置process.env.redis\u host,则使用127.0.0.1作为主机地址。您必须解析配置文件或通过-e KEY=VAL
当您运行docker compose或只是编辑行时REDIS_HOST: ${REDIS_HOST}
更换${REDIS_HOST}
使用docker-compose.yml文件中的服务名(redis)或…,有很多可能性。如果您想知道redis容器的真实ip,只需与docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <CONTAINER_ID/NAME>
9ceoxa922#
安装redis
sudo apt-get install redis-server
运行命令检查是否一切正常sudo service redis-server status
如果你得到消息redis-server is running
那就解决了lstz6jyr3#
我终于弄明白了。我把heroku里的redis插件从heroku redis改成了redis,然后改了这一行
到