for some reason i seem to be having difficulty pointing bee-queue's arena to my other docker image for redis. just wondering if anyone has any experience with this.
this is the pattern of the config i'm using
{
"queues": [
{
"hostId": "eyeshade-workers",
"type": "bee",
"name": "settlement-report",
"redis": "redis://redis:3011"
},
...
I have also tried
{
"queues": [
{
"hostId": "eyeshade-workers",
"type": "bee",
"name": "settlement-report",
"redis": {
"url": "redis://redis:3011"
}
},
{
"queues": [
{
"hostId": "eyeshade-workers",
"type": "bee",
"name": "settlement-report",
"url": "redis://redis:3011"
},
here is my redis and arena docker-compose file are here:
version: "2.1"
networks:
ledger:
driver: bridge
services:
redis:
container_name: ledger-redis
image: redis:latest
ports:
- "3011:6379"
networks:
- ledger
arena:
container_name: worker-arena
image: mixmaxhq/arena:latest
networks:
- ledger
ports:
- "4567:4567"
depends_on:
- redis
- eyeshade-worker
volumes:
- ./queue/index.json:/opt/arena/src/server/config/index.json
but i continuously get this error saying that it is trying to connect to the default :/
worker-arena | (node:42) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
worker-arena | (node:42) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
worker-arena | (node:42) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
worker-arena | events.js:182
worker-arena | throw er; // Unhandled 'error' event
worker-arena | ^
worker-arena |
worker-arena | Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
worker-arena | at Object.exports._errnoException (util.js:1016:11)
worker-arena | at exports._exceptionWithHostPort (util.js:1039:20)
worker-arena | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1138:14)
worker-arena | [nodemon] app crashed - waiting for file changes before starting...
Related
i'm getting this error while connecting node to mongodb:
techworld-js-docker-demo-app-mongo-express-1 | Could not connect to database using connectionString: mongodb://admin:password#mongodb:27017/"
techworld-js-docker-demo-app-mongo-express-1 | (node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [Error: connect ECONNREFUSED 172.19.0.3:27017
techworld-js-docker-demo-app-mongo-express-1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
techworld-js-docker-demo-app-mongo-express-1 | name: 'MongoNetworkError'
techworld-js-docker-demo-app-mongo-express-1 | }]
techworld-js-docker-demo-app-mongo-express-1 | at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
techworld-js-docker-demo-app-mongo-express-1 | at Pool.emit (events.js:314:20)
techworld-js-docker-demo-app-mongo-express-1 | at /node_modules/mongodb/lib/core/connection/pool.js:564:14
techworld-js-docker-demo-app-mongo-express-1 | at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
techworld-js-docker-demo-app-mongo-express-1 | at /node_modules/mongodb/lib/core/connection/connect.js:32:7
techworld-js-docker-demo-app-mongo-express-1 | at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
techworld-js-docker-demo-app-mongo-express-1 | at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
techworld-js-docker-demo-app-mongo-express-1 | at Object.onceWrapper (events.js:421:26)
techworld-js-docker-demo-app-mongo-express-1 | at Socket.emit (events.js:314:20)
techworld-js-docker-demo-app-mongo-express-1 | at emitErrorNT (internal/streams/destroy.js:92:8)
techworld-js-docker-demo-app-mongo-express-1 | at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
techworld-js-docker-demo-app-mongo-express-1 | at processTicksAndRejections (internal/process/task_queues.js:84:21)
techworld-js-docker-demo-app-mongo-express-1 | (node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
Docker-compose.yaml file :
version: '3'
services:
my-app:
image: nihalchandra/myapp:4.0
ports:
- 3000:3000
mongodb:
image: mongo
restart: always
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
volumes:
- mongo-data:/data/db
mongo-express:
image: mongo-express
restart: always # fixes MongoNetworkError when mongodb is not ready when mongo-express starts
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
mongo-data:
driver: local
done this in server.js file:
let mongoUrlLocal = "mongodb://admin:password#localhost:27017";
let mongoUrlDocker = "mongodb://admin:password#mongodb";
i tried changing the server but it still didnt work
You should add into your my-app definition a link to database:
my-app:
image: nihalchandra/myapp:4.0
ports:
- 3000:3000
links:
- mongodb:mongodb
Then your application will be able to connect to MongoDB using mongodb as a hostname.
More details about this you can find in documentation: https://docs.docker.com/compose/networking/
Good day everyone. I set up a simple next.js page to get data from Strapi via API. It runs fine locally. But when I run them in Docker, I got a ECONNREFUSED error.
here is the error:
error - TypeError: fetch failed
frontend | at Object.fetch (node:internal/deps/undici/undici:11118:11)
frontend | at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
frontend | at async getStaticProps (webpack-internal:///./pages/index.tsx:38:17)
frontend | at async Object.renderToHTML (/usr/local/frontend/node_modules/next/dist/server/render.js:384:20)
frontend | at async doRender (/usr/local/frontend/node_modules/next/dist/server/base-server.js:708:34)
frontend | at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (/usr/local/frontend/node_modules/next/dist/server/base-server.js:813:28)
frontend | at async /usr/local/frontend/node_modules/next/dist/server/response-cache/index.js:80:36 {
frontend | cause: Error: connect ECONNREFUSED 127.0.0.1:1337
frontend | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16) {
frontend | errno: -111,
frontend | code: 'ECONNREFUSED',
frontend | syscall: 'connect',
frontend | address: '127.0.0.1',
frontend | port: 1337
frontend | },
frontend | page: '/'
frontend | }
And here is the code in Next.js:
export async function getStaticProps() {
const res = await fetch('http://localhost:1337/api/posts')
const data = await res.json()
return {
props: { data },
}
}
My docker-compose.yml:
version: '3.8'
services:
backend:
container_name: backend
build:
context: backend
dockerfile: .docker/dev.dockerfile
image: backend:latest
# restart: unless-stopped
env_file: ./backend/.env
volumes:
- strapi_data:/usr/local/backend/strapi/.tmp
- strapi_upload:/usr/local/backend/strapi/public/uploads
ports:
- '1337:1337'
frontend:
container_name: frontend
build:
context: frontend
dockerfile: .docker/dev.dockerfile
image: frontend:latest
# restart: unless-stopped
env_file: ./frontend/.env
volumes:
- node_modules_next:/usr/local/frontend/node_modules
- next_folder:/usr/local/frontend
ports:
- '3000:3000'
depends_on:
- 'backend'
volumes:
node_modules_next:
next_folder:
strapi_data:
strapi_upload:
I followed this guide to set up the middleware in Strapi:
export default [
"strapi::errors",
"strapi::security",
// 'strapi::cors',
{
name: "strapi::cors",
config: {
origin: "*",
methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"],
headers: ["Content-Type", "Authorization", "Origin", "Accept"],
keepHeaderOnError: true,
},
},
"strapi::poweredBy",
"strapi::logger",
"strapi::query",
"strapi::body",
"strapi::session",
"strapi::favicon",
"strapi::public",
];
But it doesn't work.
I also tried to change getStaticProps to getServerSideProps, no luck.
I figured it out myself. Once I run docker-compose on my server, I change the fetch link from http://localhost:1337/api/posts to http://{ip}:1337/api/posts
And it works.
There are multiple services that I had been trying to run (redis, front-end, back-end and elastic-search) and I was not able to connect to the elastic search service. I even tried giving a static ip for the service. (The networking part is currently commented out in the docker file attached). I tried changing the images and it still was not working.
When I tested ES locally using curl localhost:9200/_cat/health as I have mapped the container port locally it gives me that the cluster is green. I could connect to the other services like redis without issues. As with redis, I am using the service name, elasticsearch to connect it to the back-end service. Following is my docker-compose.yml file.
version: '3'
services:
arc-external:
image: arc-external
build:
context: ./arc-development-branch/arc-external
ports:
- '4201:4201'
# networks:
# - vpcbr
redis:
image: redis:3.2.11-alpine
ports:
- '6379:6379'
# networks:
# - vpcbr
elasticsearch:
image: elasticsearch:2
ports:
- '9200:9200'
- '9300:9300'
environment:
- node.name=elasticsearch
- cluster.name=datasearch
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- cluster.initial_master_nodes=elasticsearch
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./data/elastic:/usr/share/elasticsearch/data
# networks:
# vpcbr:
# ipv4_address: 10.5.0.4
api-external:
image: api-external
build: .
ports:
- '3001:3001'
depends_on:
- redis
- elasticsearch
# networks:
# - vpcbr
# networks:
# vpcbr:
# driver: bridge
# ipam:
# config:
# - subnet: 10.5.0.0/16
# gateway: 10.5.0.1
This is the exact error that I am getting when running docker compose-up
api-external_1 | 2021-03-09 20:41:46.3253 - info: Finished setting up log directories
api-external_1 | 2021-03-09 20:41:46.3514 - info: Connection successful to mongodb # mongodb://10.0.0.44:27017/arc
api-external_1 | 2021-03-09 20:41:46.3764 - info: Connection successful to redis at: host: redis port: 6379
api-external_1 | Elasticsearch ERROR: 2021-03-09T20:41:46Z
api-external_1 | Error: Request error, retrying
api-external_1 | HEAD http://elasticsearch:9200/ => connect ECONNREFUSED 172.24.0.4:9200
api-external_1 | at Log.error (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/log.js:226:56)
api-external_1 | at checkRespForFailure (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/transport.js:259:18)
api-external_1 | at HttpConnector.<anonymous> (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/connectors/http.js:164:7)
api-external_1 | at ClientRequest.wrapper (/usr/src/app/api-external/node_modules/lodash/lodash.js:4935:19)
api-external_1 | at ClientRequest.emit (events.js:198:13)
api-external_1 | at ClientRequest.EventEmitter.emit (domain.js:448:20)
api-external_1 | at Socket.socketErrorListener (_http_client.js:401:9)
api-external_1 | at Socket.emit (events.js:198:13)
api-external_1 | at Socket.EventEmitter.emit (domain.js:448:20)
api-external_1 | at emitErrorNT (internal/streams/destroy.js:91:8)
api-external_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
api-external_1 | at process._tickCallback (internal/process/next_tick.js:63:19)
api-external_1 |
api-external_1 | Elasticsearch WARNING: 2021-03-09T20:41:46Z
api-external_1 | Unable to revive connection: http://elasticsearch:9200/
api-external_1 |
api-external_1 | Elasticsearch WARNING: 2021-03-09T20:41:46Z
api-external_1 | No living connections
api-external_1 |
api-external_1 | 2021-03-09 20:41:46.3844 - error: Error: Failed to connect to elasticsearch # elasticsearch:9200
api-external_1 | at exports.esClient.ping (/usr/src/app/api-external/dist/setup/elastic-search.js:33:46)
api-external_1 | at respond (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/transport.js:327:9)
api-external_1 | at sendReqWithConnection (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/transport.js:226:7)
api-external_1 | at next (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/connection_pool.js:214:7)
api-external_1 | at process._tickCallback (internal/process/next_tick.js:61:11)
api-external_1 | 2021-03-09 20:41:46.3854 - error: Error: No Living connections
api-external_1 | at sendReqWithConnection (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/transport.js:226:15)
api-external_1 | at next (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/connection_pool.js:214:7)
api-external_1 | at process._tickCallback (internal/process/next_tick.js:61:11)
api-external_1 | npm ERR! code ELIFEC
Frankly, I searched a lot and were not able to debug it. Any help would be appreciated.
I was able to figure out the answer. The thing stating as depends_on does not wait the services to completely up. Here, api-external does get start up as soon as the redis and elasticsearch starts. However, elasticsearch need a bit time to configure everything so restarting the service will do the trick.
More permanent solution is to write a script that would wait until the elasticsearch is up completely before starting the api-external service
I have been trying to build the classical voting app. But my redis and mongodb are giving same error. I have to connect my redis to my node app and i have tried the following:
//redis
const redisoption = {
host: "redis",
port: 6379
};
.
//redis
const redisoption = {
host: "0.0.0.0",
port: 6379
};
docker-compose.yml
version: "3"
services:
vote:
image: adityagaddhyan/voting-app
ports:
- "3000:3000"
depends_on:
- redis
networks:
- front-end
- back-end
volumes:
- ./vote:/app
result:
image: adityagaddhyan/voting-result
ports:
- "3011:3011"
depends_on:
- mongodb
networks:
- back-end
- front-end
volumes:
- ./result:/app
worker:
image: adityagaddhyan/service-worker
depends_on:
- redis
- mongodb
networks:
- back-end
volumes:
- ./worker:/app
redis:
image : redis
command: ["redis-server", "--bind", "0.0.0.0", "--port", "6379"
ports:
- "6379:6379"
networks:
- back-end
volumes:
- ./redis:/app
mongodb:
image: mongo
ports:
- "27017:27017"
volumes:
- ./db:/app
volumes:
db-data:
networks:
front-end:
back-end:
I have tried all sort of combination.
I am majorily getting two kind of error.
if i use host: "0.0.0.0", i get
Error: Redis connection to 0.0.0.0:6379 failed - connect ECONNREFUSED 0.0.0.0:6379
vote_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
vote_1 | Emitted 'error' event on RedisClient instance at:
vote_1 | at RedisClient.on_error (/usr/src/app/node_modules/redis/index.js:341:14)
vote_1 | at Socket.<anonymous> (/usr/src/app/node_modules/redis/index.js:222:14)
vote_1 | at Socket.emit (events.js:315:20)
vote_1 | at emitErrorNT (internal/streams/destroy.js:92:8)
vote_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
vote_1 | at processTicksAndRejections (internal/process/task_queues.js:84:21) {
vote_1 | errno: 'ECONNREFUSED',
vote_1 | code: 'ECONNREFUSED',
vote_1 | syscall: 'connect',
vote_1 | address: '0.0.0.0',
vote_1 | port: 6379
vote_1 | }
and if i use host:redis, i get:
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
worker_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
worker_1 | Emitted 'error' event on RedisClient instance at:
worker_1 | at RedisClient.on_error (/usr/src/app2/node_modules/redis/index.js:341:14)
worker_1 | at Socket.<anonymous> (/usr/src/app2/node_modules/redis/index.js:222:14)
worker_1 | at Socket.emit (events.js:315:20)
worker_1 | at emitErrorNT (internal/streams/destroy.js:92:8)
worker_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
worker_1 | at processTicksAndRejections (internal/process/task_queues.js:84:21) {
worker_1 | errno: 'ECONNREFUSED',
worker_1 | code: 'ECONNREFUSED',
worker_1 | syscall: 'connect',
worker_1 | address: '127.0.0.1',
worker_1 | port: 6379
worker_1 | }
vote_1 | /usr/src/app/app.js:28
vote_1 | host: back-end,
vote_1 | ^
I am new to docker and unable to solve this problem. please help.
I am having an issue with my nodejs-redis-docker-compose application. The application ran normally for a few times. But later, it started giving me the following error. I am using redis-server v3.2.100 docker 18.09.1 build 4c52b90. I uninstalled docker and redis and re-installed them but I still have the same problem. Here is the error.
REDIS_DB | 1:C 22 Jan 2019 02:24:00.208 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
REDIS_DB | 1:C 22 Jan 2019 02:24:00.208 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=1, just started
REDIS_DB | 1:C 22 Jan 2019 02:24:00.208 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
REDIS_DB | 1:M 22 Jan 2019 02:24:00.210 * Running mode=standalone, port=6379.
REDIS_DB | 1:M 22 Jan 2019 02:24:00.210 # WARNING: The TCP backlog setting of
511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
REDIS_DB | 1:M 22 Jan 2019 02:24:00.210 # Server initialized
REDIS_DB | 1:M 22 Jan 2019 02:24:00.210 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues
with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
REDIS_DB | 1:M 22 Jan 2019 02:24:00.211 * DB loaded from disk: 0.001 seconds
REDIS_DB | 1:M 22 Jan 2019 02:24:00.211 * Ready to accept connections
MAIN_API | [nodemon] starting `node --trace-warnings index.js`
MAIN_API | Unhandled Rejection at: Promise Promise {
MAIN_API | <rejected> { Error: connect ECONNREFUSED 127.0.0.1:6379
MAIN_API | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
MAIN_API | errno: 'ECONNREFUSED',
MAIN_API | code: 'ECONNREFUSED',
MAIN_API | syscall: 'connect',
MAIN_API | address: '127.0.0.1',
MAIN_API | port: 6379 } } reason: Error: connect ECONNREFUSED 127.0.0.1:6379
MAIN_API | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
MAIN_API | Unhandled Rejection at: Promise Promise {
MAIN_API | <rejected> { Error: connect ECONNREFUSED 127.0.0.1:6379
MAIN_API | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
MAIN_API | errno: 'ECONNREFUSED',
MAIN_API | code: 'ECONNREFUSED',
MAIN_API | syscall: 'connect',
MAIN_API | address: '127.0.0.1',
MAIN_API | port: 6379 } } reason: Error: connect ECONNREFUSED 127.0.0.1:6379
MAIN_API | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
MAIN_API | ✓-- Redis client pid=>37 Connected
MAIN_API | --- Redis server pid=>37 is ready
MAIN_API | ~~~ Testing local redis storage...
MAIN_API | ✓-- Redis pid=>37 startup test succeeded
MAIN_API | ✓-- MongoDB pid=>37 Connected
MAIN_API | ✓-- Server pid=>48 running at port: 5555
MAIN_API | Unhandled Rejection at: Promise Promise {
MAIN_API | <rejected> { Error: connect ECONNREFUSED 127.0.0.1:6379
MAIN_API | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
MAIN_API | errno: 'ECONNREFUSED',
MAIN_API | code: 'ECONNREFUSED',
MAIN_API | syscall: 'connect',
MAIN_API | address: '127.0.0.1',
MAIN_API | port: 6379 } } reason: Error: connect ECONNREFUSED 127.0.0.1:6379
MAIN_API | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
MAIN_API | Unhandled Rejection at: Promise Promise {
MAIN_API | <rejected> { Error: connect ECONNREFUSED 127.0.0.1:6379
MAIN_API | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
MAIN_API | errno: 'ECONNREFUSED',
MAIN_API | code: 'ECONNREFUSED',
MAIN_API | syscall: 'connect',
MAIN_API | address: '127.0.0.1',
MAIN_API | port: 6379 } } reason: Error: connect ECONNREFUSED 127.0.0.1:6379
MAIN_API | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
MAIN_API | ✓-- Redis client pid=>48 Connected
MAIN_API | --- Redis server pid=>48 is ready
MAIN_API | ~~~ Testing local redis storage...
MAIN_API | ✓-- Redis pid=>48 startup test succeeded
MAIN_API | ✓-- MongoDB pid=>48 Connected
The error is thrown when the nodejs application is starting but it get connected to the redis server by the time the application is up and running.
Redis console log
[16308] 21 Jan 18:03:25.425 # Server started, Redis version 3.2.100
[16308] 21 Jan 18:03:25.426 * DB loaded from disk: 0.000 seconds
[16308] 21 Jan 18:03:25.426 * The server is now ready to accept connections on port 6379
I ran netstat -a -n -o on my console and here is the relevant part (I believe).
TCP 0.0.0.0:6379 0.0.0.0:0 LISTENING 16308
TCP [::]:6379 [::]:0 LISTENING 16308
The following are my files.
./Dockerfile
FROM node:10.15.0
RUN mkdir -p ./usr/src/MainAPI
WORKDIR /usr/src/MainAPI
COPY ./ ./usr/src/MainAPI
RUN npm install
ARG NODE_VERSION=10.15.0
./docker-compose.yml
version: "3.7"
services:
# Redis
redis:
container_name: REDIS_DB
image: redis
ports:
- "6378:6379"
# API
main-api:
container_name: MAIN_API
build: ./
command: ["npm", "start"]
working_dir: /usr/src/MainAPI
ports:
- "5555:5555"
volumes:
- ./:/usr/src/MainAPI
./redisClient.js
const Redis = require("ioredis");
//module.exports = require("redis").createClient(...); => same errors with this module as well
module.exports = new Redis('redis://redis:6379/0');
//module.exports = new Redis('redis://127.0.0.1:6379/0'); =>crashes the app
//module.exports = new Redis('redis://redis:6378/0'); =>crashes the app also
./app.js
"use strict";
const express = require("express");
const { red, green, cyan, yellow } = require("kleur");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const morgan = require("morgan");
const cors = require("cors");
const path = require("path");
const helmet = require("helmet");
const redisClient = require("./redisClient");
const { CLIENT_BASE_URL, API_BASE_URL, MONGO_URI } = process.env;
const isProduction = process.env.NODE_ENV === "production";
const server = express();
const whitelist = [CLIENT_BASE_URL, API_BASE_URL];
const corsOptions = {
origin: function(origin, callback) {
if (whitelist.indexOf(origin) !== -1 || !origin) {
return callback(null, true);
} else {
return callback(new Error("Not allowed by CORS"), false);
}
}
};
server.use(helmet());
if (isProduction) {
server.use(cors(corsOptions));
server.use(morgan("combined"));
} else {
server.use(morgan("dev"));
}
server.set("trust proxy", 1);
/**
* Test redis storage on deploy
*/
redisClient.on("connect", function() {
console.log(
green("✓-- ") + "Redis client pid=>" + process.pid + " Connected"
);
});
redisClient.on("ready", function() {
console.log(cyan("--- Redis server pid=>" + process.pid + " is ready"));
console.log(cyan("~~~ Testing local redis storage..."));
const testKey = "key";
const testValue = "value";
redisClient.set(testKey, testValue).then(() => {
redisClient.get(testKey, (error, result) => {
if (error) {
console.log(red("✗-- An error occured while testing redis storage"));
console.error(error);
return process.exit(1);
}
if (!result || result !== testValue) {
console.log(
red(
"✗-- Redis pid=>" + process.pid + " did not pass the startup test"
)
);
console.error(result);
return process.exit(1);
}
console.log(
green("✓-- Redis pid=>" + process.pid + " startup test succeeded")
);
});
});
});
redisClient.on("error", function(err) {
console.log(red("✗-- Something went wrong with redis"));
console.error(err);
process.exit(1);
});
redisClient.on("end", function() {
console.log(
yellow("--- ") + "Redis client pid=>" + process.pid + " closed connection"
);
});
mongoose.set("useCreateIndex", true);
mongoose.Promise = global.Promise;
mongoose
.connect(
MONGO_URI,
{
useNewUrlParser: true
}
)
.then(() => {
console.log(green("✓-- ") + "MongoDB pid=>" + process.pid + " Connected");
})
.catch(err => {
console.log(red("✗--Database Connection"));
console.error(err);
process.exit(1);
});
server.use(
bodyParser.urlencoded({
extended: false
})
);
server.use(bodyParser.json());
require("./controlers/home")(server);
require("./controlers/user")(server);
require("./controlers/admin")(server);
if (isProduction) {
server.use(express.static("client/build"));
server.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
module.exports = server;
I did not call .listen(...) on the server because it is exported to another file (index.js).
Thank you
I am responding to my own question. I found what is causing the issue in my code. In fact, I started using npm agenda, which uses MongoDB, for scheduling some tasks in my application. I later switched to npm bull, which is using Redis. I created the queue instance like the following.
const Queue = require("bull");
const emailQueue = new Queue("emails");
while it should be
const Queue = require("bull");
const emailQueue = new Queue("emails", {
redis: { port: 6379, host: "redis-server" }
});
Because I did not specify the redis settings for the module, it was not able to connect to the server. I totally forgot that I had another module that is using redis. The reason the app seemed to be working is because express get connected to redis while bull did not. I would figure that out if I had tried to use a feature that bull is part of the process. I also simplified my Dockerfile and docker-compose.yml files to include only the necessary steps.
/Dockerfile
FROM node:alpine # create image with NodeJS version alpine included
WORKDIR /usr/app # set working directory of the app to be /usr/app/. create folder /usr/app if it does not exist already. The following operations would then happen inside /usr/app
COPY ./package.json ./ # copy package.json file into current directory, which is /usr/app according to previous step
RUN npm install # install dependencies
COPY ./ ./ # add the rest of the files to /usr/app
CMD ["npm", "start"] # run 'npm start' when the container is starting
/docker-compose.yml
version: "3"
services:
# Redis
redis-server: # the name of the redis service becomes the host name in my application
container_name: REDIS_SERVER
image: 'redis'
# API
main-api:
container_name: MAIN_API
build: ./
ports:
- "5555:5555"
I did not need anything else for the application to work. By simply giving the service name to ioredis in my application, the module will figure out where to find the server. That is said here in links section. In my case, the server name is redis-server so the hostname for ioredis and bull would also be redis-server and the port number 6379. Thank you #DavidMaze #yeaseol, and ...
The port is bound to 6378, so you need to access 6378.
And can you use hostname redis? (Did you set it?)
Try this:
module.exports = new Redis('redis://{docker-server-hostname}:6378');
or
The best option is to set a link to docker-compose.
main-api:
container_name: MAIN_API
build: ./
command: ["npm", "start"]
working_dir: /usr/src/MainAPI
ports:
- "5555:5555"
volumes:
- ./:/usr/src/MainAPI
links:
- {your-redis-container-name}
Try this:
module.exports = new Redis('redis://{your-redis-container-name}:6379');