サーバーとデータベースを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);
});
}
ログクラッシュする前にサーバーから取得しています:
api_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データベースに接続できますが、何らかの理由でRedisに接続できなくなっているようです。私はNode-Mongo-Redisの設定を意図した様々なpremadeドッカーの作成ファイルを試しましたが、これまでに何の仕事もしていませんでした。これについての助けに感謝します。