/ 以下代码片段来自docker-compose.yml文件 /
version: "3"
services:
app:
container_name: app
restart: always
build: .
ports:
- "3006:3006"
# deploy:
# mode: replicated
#replicas: 6
networks:
- overlay
depends_on:
- redis
- mongo
- mysql
links:
- mongo
- redis
- mysql
mongo:
container_name: mongo
networks:
- overlay
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
redis:
image: redis:latest
networks:
- overlay
volumes:
- ./data:/redisdata/redisdb
ports:
- "6379:6379"
logging:
driver: "json-file"
options:
max-size: "100MB"
max-file: "3"
mysql:
image: mysql:latest
networks:
- overlay
ports:
- "3307:3307"
environment:
- 'MYSQL_ROOT_PASSWORD=******'
- 'MYSQL_DATABASE=iconnect_test_demo'
- 'MYSQL_USER=root'
- 'MYSQL_PASSWORD=******'
networks:
overlay:
在docker-compose.yml文件上方执行时,所有4个容器都已创建,但无法连接到Redis数据库并收到以下异常。尝试用localhost替换Redis,但仍然没有运气。但是如果我在Docker外部独立运行节点程序它连接到依赖于容器内部的redis数据库。
在执行过程中导致以下错误。
[ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379
app | at Object.exports._errnoException (util.js:1050:11)
app | at exports._exceptionWithHostPort (util.js:1073:20)
app | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1093:14)
Complete Error Log is pasted in below url.
https://pastebin.com/0NvC3nz6
In Node File trying to connect to redis server in the following way.
var Redis = require('ioredis');
var redisadapter = require('socket.io-redis');
/* For maintaining connectivity across the multiple nodes */
io.adapter(redisadapter({ host: 'redis', port: 6379 }));
#io.adapter(redisadapter({ host: 'localhost', port: 6379 }));
server.listen(3006);
console.log("server running on 3006");
/* Redis configuration */
var redis_address = 'redis://redis:6379';
#var redis_address = 'redis://localhost:6379';
var redis = new Redis(redis_address);
redis.on('ECONNREFUSED', function(err) {
logger.error('Redis error: ' + err);
} );
/ Docker文件 /
FROM node:7
WORKDIR /app
COPY . /app
CMD node server1.js
EXPOSE 3006
答案 0 :(得分:1)
如果您在日志文件中查看此特定行:
app | [ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379
它告诉您应用程序容器无法连接到Redis容器。 Redis实例的IP地址在端口6379上为 127.0.0.1 。但是,在这里,您的Redis服务器不在app容器上运行,而是在redis容器上运行。您的代码或配置中有问题。有关更多详细信息,请查阅所用库的文档。此外,请确保每次更改代码中的内容时都重新构建应用docker映像!
答案 1 :(得分:0)
我在can't connect to redis through node app, both in dockers中读到
启动对象的操作应
const Redis = require('ioredis');
const redis = new Redis({ host: 'redis' });
(删除redis://前缀并添加host:
作为对象键)
如果这样不起作用,请尝试添加redis容器的容器名称并以其名称进行调用
container_name: svc-redis
并将主机配置为svc-redis:6379
答案 2 :(得分:0)
谢谢,我在日志中错过了它。它解决了。