그래서 서버와 데이터베이스를 docker-compose를 사용하여 docker 컨테이너에 넣으려고합니다.NodeJS 서버의 ENOTFOUND와 Docker를 가진 Redis
나는 현재와 같은 모습을 가지고있는 고정 표시기-작성 파일 :
databses에 연결version: "2"
services:
api:
build: .
ports:
- "3000:3000"
command: npm run-script docker
networks:
- backend
mongodb:
image: mongo:latest
ports:
- "27017:27017"
networks:
- backend
redis:
image: redis:latest
ports:
- "6379:6379"
networks:
- backend
networks:
backend:
내 코드는 다음과 같습니다
/**
* Init the Redis db.
* @returns {*|Promise}
*/
init: function (isMaster=false) {
return new Promise(function (resolve, reject) {
logger.logInfo('(rdb.init) Trying to connect to Redis database');
client = redis.createClient("redis://redis:6379");
client.on("error", err => {
logger.logFatal('(rdb.init) Could not connect to redis database - ' + err);
reject(err);
});
client.on("connect",() => {
logger.logInfo('(rdb.init) Successfully connected to redis database');
if (config.CLEAR_REDIS_COLLECTIONS && isMaster) {
logger.logWarn('(rdb.init) CLEAR_REDIS_COLLECTIONS = ' + config.CLEAR_REDIS_COLLECTIONS);
dropCollections().then(() => resolve()).catch(err => reject(err));
} else {
resolve();
}
});
});
}
그리고
/*
*Connect to the database
*/
function init(isMaster=false) {
logger.logInfo('(mdb.init) Trying to connect to the database @' + config.DB_URL);
mongoose.Promise = Promise;
// Connect to the db
return mongoose.connect("mongodb://mongodb:27017/stuber", {useMongoClient: true}).then(() => {
logger.logInfo('(mdb.init) Successfully connected to the database');
if (config.CLEAR_MONGO_COLLECTIONS && isMaster) {
logger.logWarn('(mdb.init) CLEAR_MONGO_COLLECTIONS = ' + config.CLEAR_MONGO_COLLECTIONS);
return dropCollections();
} else {
return Promise.resolve();
}
}, err => {
logger.logFatal('(mdb.init) Could not connect to the database - ' + err);
return Promise.reject(err);
});
}
로그 서버가 고장 나기 전에 서버에서 빠져 나옵니다.
,210api_1 | [2017-12-29 14:25:36.711] [INFO] Stuber - [Master] ==================================| v0.4.0 |================================== api_1 | [2017-12-29 14:25:36.770] [WARN] Stuber - [Master] (tokenAuth) Token authentication BYPASS = false api_1 | [2017-12-29 14:25:38.594] [INFO] Stuber - [Master] (io.createImageDirs) User image path = /app/public/images/users/ api_1 | [2017-12-29 14:25:38.595] [INFO] Stuber - [Master] (io.createImageDirs) Vehicle image path = /app/public/images/vehicles/ api_1 | [2017-12-29 14:25:38.595] [INFO] Stuber - [Master] (io.createImageDirs) Creating "public" directory api_1 | [2017-12-29 14:25:38.596] [INFO] Stuber - [Master] (io.createImageDirs) Creating "images" directory api_1 | [2017-12-29 14:25:38.596] [INFO] Stuber - [Master] (io.createImageDirs) Creating "users" directory api_1 | [2017-12-29 14:25:38.597] [INFO] Stuber - [Master] (io.createImageDirs) Creating "vehicles" directory api_1 | [2017-12-29 14:25:38.597] [INFO] Stuber - [Master] (rdb.init) Trying to connect to Redis database api_1 | [2017-12-29 14:25:38.682] [INFO] Stuber - [Master] (rdb.init) Successfully connected to redis database api_1 | [2017-12-29 14:25:38.682] [WARN] Stuber - [Master] (rdb.init) CLEAR_REDIS_COLLECTIONS = true api_1 | [2017-12-29 14:25:38.781] [INFO] Stuber - [Master] (rdb.dropCollections) Cleared Redis database api_1 | [2017-12-29 14:25:38.782] [INFO] Stuber - [Master] (mdb.init) Trying to connect to the database @mongodb://user:[email protected]:xxx/stuber mongodb_1 | 2017-12-29T14:25:38.808+0000 I NETWORK [listener] connection accepted from 172.19.0.2:33810 #2 (1 connection now open) mongodb_1 | 2017-12-29T14:25:38.834+0000 I NETWORK [conn2] received client metadata from 172.19.0.2:33810 conn: { driver: { name: "nodejs", version: "2.2.33" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.4.0-1039-aws" }, platform: "Node.js v7.10.1, LE, mongodb-core: 2.1.17" } api_1 | [2017-12-29 14:25:38.847] [INFO] Stuber - [Master] (mdb.init) Successfully connected to the database api_1 | [2017-12-29 14:25:38.847] [INFO] Stuber - [Master] (server) Create kue job queues api_1 | [2017-12-29 14:25:38.934] [INFO] Stuber - [Master] (kue.init) Master worker is being assigned processing of jobs api_1 | [2017-12-29 14:25:38.947] [INFO] Stuber - [Master] (server.init) Retrieving all system IDs from database api_1 | poop api_1 | events.js:163 api_1 | throw er; // Unhandled 'error' event api_1 | ^ api_1 | api_1 | Error: Redis connection to redis://redis:6379 failed - getaddrinfo ENOTFOUND redis://redis redis://redis:6379 api_1 | at errnoException (dns.js:28:10) api_1 | at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:73:26)
무시 [2017년 12월 29일 14 : 25 : 38.782] [INFO] STUBER - [마스터 (mdb.init) 데이터베이스 @mongodb 연결하려고 : // 사용자 : XXX @의 xxx라고 : xxx/stuber 정확한 주소를 로깅하지 않기 때문에 코드는 실제로 연결되어있는 것을 보여줍니다.
따라서 로그에 따라 서버는 Mongo 및 Redis 데이터베이스에 연결할 수 있지만 어떤 이유로 Redisis 연결이 끊어지는 것 같습니다. 나는 Node-Mongo-Redis 설정을위한 다양한 premade docker-compose 파일을 시도했지만 지금까지는 아무 것도 시도하지 못했습니다. 이것에 대한 도움을 주시면 감사하겠습니다.