Error in docker-compose server is not defined - node.js

I'm trying to run a very simple docker-compose.yml, but am getting a "ReferenceError: server is not defined" from index.js in the client container.
| /app/index.js:6
node-app_1 | host: redis-server,
node-app_1 | ^
Here's the code:
index.js
const express = require('express');
const redis = require('redis');
const app = express();
const client = redis.createClient({
host: redis-server,
port: 6379
});
client.set('visits', 0);
app.get('/', (req,res) => {
client.get('visits', (err,visits) => {
res.send('Number of visit is: ' + visits);
client.set('visits', parseInt(visits) + 1);
})
})
app.listen(8081, () =>{
console.log('Listening on port 8081');
})
docker-compose.yml
version: '3'
services:
redis-server:
image: 'redis'
node-app:
build: .
ports:
- '4001:8081'
Output of docker-compose up
redis-server_1 | 1:C 07 Nov 2020 16:52:30.899 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-server_1 | 1:C 07 Nov 2020 16:52:30.900 # Redis version=6.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
redis-server_1 | 1:C 07 Nov 2020 16:52:30.900 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis-server_1 | 1:M 07 Nov 2020 16:52:30.901 * Running mode=standalone, port=6379.
redis-server_1 | 1:M 07 Nov 2020 16:52:30.902 # Server initialized
redis-server_1 | 1:M 07 Nov 2020 16:52:30.902 * Loading RDB produced by version 6.0.9
redis-server_1 | 1:M 07 Nov 2020 16:52:30.902 * RDB age 180 seconds
redis-server_1 | 1:M 07 Nov 2020 16:52:30.902 * RDB memory usage when created 0.77 Mb
redis-server_1 | 1:M 07 Nov 2020 16:52:30.903 * DB loaded from disk: 0.000 seconds
redis-server_1 | 1:M 07 Nov 2020 16:52:30.903 * Ready to accept connections
node-app_1 |
node-app_1 | > start
node-app_1 | > node index.js
node-app_1 |
node-app_1 | /app/index.js:6
node-app_1 | host: redis-server,
node-app_1 | ^
node-app_1 |
node-app_1 | ReferenceError: server is not defined
node-app_1 | at Object.<anonymous> (/app/index.js:6:17)
node-app_1 | at Module._compile (node:internal/modules/cjs/loader:1083:30)
node-app_1 | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
node-app_1 | at Module.load (node:internal/modules/cjs/loader:948:32)
node-app_1 | at Function.Module._load (node:internal/modules/cjs/loader:789:14)
node-app_1 | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:72:12)
node-app_1 | at node:internal/main/run_main_module:17:47
node-app_1 | npm ERR! code 1
node-app_1 | npm ERR! path /app
node-app_1 | npm ERR! command failed
node-app_1 | npm ERR! command sh -c node index.js
node-app_1 |
node-app_1 | npm ERR! A complete log of this run can be found in:
node-app_1 | npm ERR! /root/.npm/_logs/2020-11-07T16_52_31_324Z-debug.log
visits_node-app_1 exited with code 1
It doesn't find "redis-server", but according to the udemy course, it should find it because that's how it's identified in the composer.
What's the problem?

"redis-server" should be a string, and denoted by quotes:
const client = redis.createClient({
host: 'redis-server',
port: 6379
});

Related

NodeJS converting Docker Redis hostname to localhost

It seems the Redis container hostname is being converted to localhost by NodeJS.
Here are my files:
.env
REDIS_HOST=redis-eventsystem
REDIS_PORT=6379
REDIS_SECRET=secret
index.ts
// there are things above this
let Redis = require('redis');
let client : any = Redis.createClient({
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
legacyMode: true
});
client.on('error', (err : Error) : void => {
console.error(
`Redis connection error: ${err}`
);
});
client.on('connect', (err : Error) : void => {
console.info(
`Redis connection success.`
);
});
client.connect();
// there are things bellow this
docker-compose.yml
version: '3.8'
services:
eventsystem:
image: eventsystem
restart: always
depends_on:
- "redis-eventsystem"
ports:
- "80:3000"
networks:
- eventsystem
redis-eventsystem:
image: redis
command: ["redis-server", "--bind", "redis-eventsystem", "--port", "6379", "--protected-mode", "no"]
restart: always
networks:
- eventsystem
networks:
eventsystem:
driver: bridge
docker log
2022-11-21 17:50:41 eventsystem-redis-eventsystem-1 | 1:C 21 Nov 2022 20:50:41.106 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2022-11-21 17:50:41 eventsystem-redis-eventsystem-1 | 1:C 21 Nov 2022 20:50:41.106 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
2022-11-21 17:50:41 eventsystem-redis-eventsystem-1 | 1:C 21 Nov 2022 20:50:41.106 # Configuration loaded
2022-11-21 17:50:41 eventsystem-redis-eventsystem-1 | 1:M 21 Nov 2022 20:50:41.106 * monotonic clock: POSIX clock_gettime
2022-11-21 17:50:41 eventsystem-redis-eventsystem-1 | 1:M 21 Nov 2022 20:50:41.108 * Running mode=standalone, port=6379.
2022-11-21 17:50:41 eventsystem-redis-eventsystem-1 | 1:M 21 Nov 2022 20:50:41.108 # Server initialized
2022-11-21 17:50:41 eventsystem-redis-eventsystem-1 | 1:M 21 Nov 2022 20:50:41.108 * Ready to accept connections
2022-11-21 17:50:41 eventsystem-eventsystem-1 |
2022-11-21 17:50:41 eventsystem-eventsystem-1 | > eventsystem#1.0.0 start
2022-11-21 17:50:41 eventsystem-eventsystem-1 | > node index.js serve
2022-11-21 17:50:41 eventsystem-eventsystem-1 |
2022-11-21 17:50:42 eventsystem-eventsystem-1 | Application is listening at http://localhost:3000
2022-11-21 17:50:42 eventsystem-eventsystem-1 | Mon Nov 21 2022 20:50:42 GMT+0000 (Coordinated Universal Time) - Redis connection error: Error: connect ECONNREFUSED 127.0.0.1:6379
As you all can see the connection is refused for the IP 127.0.0.1 but on my application the redis is set to work on the hostname for the container which holds the redis server. I can't think of anything that may be causing this problem.
So to answer my own question, basically the problem was related to the variables passed on createClient at my code.
It seems that for some unknown reason the host and port variables need to be passed inside a variable called socket inside the createClient argument object.
So, instead of doing the usual and passing the host and port inside the argument object, you must do the following:
let client : any = Redis.createClient({
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
socket: {
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT
},
legacyMode: true
});
Hope to have helped someone else besides me.
Cheers!

why a socket server container can't reach my redis server container even if [duplicate]

This question already has answers here:
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
(7 answers)
Closed 9 months ago.
i have this docker compose that connects my redis server with my socket server that has redis client
version: "3.8"
services:
redis-server:
image: "redis"
volumes:
- express-chat-vol:/express-chat/
networks:
- express-chat-net
ports:
- 6379:6379
socket-server:
build:
context: ./server/
dockerfile: Dockerfile.socketServer
ports:
- 5000:5000
networks:
- express-chat-net
volumes:
express-chat-vol:
networks:
express-chat-net:
but socket server can't access the redis server and bring up this error and i'm stuck for 2 days now , how can i solve it ?
Starting token2_socket-server_1 ... done
Starting token2_redis-server_1 ... done
Attaching to token2_socket-server_1, token2_redis-server_1
redis-server_1 | 1:C 29 May 2022 01:07:22.191 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-server_1 | 1:C 29 May 2022 01:07:22.191 # Redis version=7.0.0, bits=64, commit=00000000, modified=0, pid=1, just started
redis-server_1 | 1:C 29 May 2022 01:07:22.191 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
//some logs
redis-server_1 | 1:M 29 May 2022 01:07:22.195 * Ready to accept connections
socket-server_1 | socket server is listening on port 5000
socket-server_1 | node:internal/process/promises:288
socket-server_1 | triggerUncaughtException(err, true /* fromPromise */);
socket-server_1 | ^
socket-server_1 |
socket-server_1 | Error: connect ECONNREFUSED 127.0.0.1:6379
socket-server_1 | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1195:16)
socket-server_1 | Emitted 'error' event on Commander instance at:
socket-server_1 | at RedisSocket.<anonymous> (/socketServer/node_modules/#redis/client/dist/lib/client/index.js:339:14)
socket-server_1 | at RedisSocket.emit (node:events:527:28)
socket-server_1 | at RedisSocket._RedisSocket_connect (/socketServer/node_modules/#redis/client/dist/lib/client/socket.js:127:14)
socket-server_1 | at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
socket-server_1 | at async Commander.connect (/socketServer/node_modules/#redis/client/dist/lib/client/index.js:163:9)
socket-server_1 | at async connectRedisClient (/socketServer/socketServer.js:19:3) {
socket-server_1 | errno: -111,
socket-server_1 | code: 'ECONNREFUSED',
socket-server_1 | syscall: 'connect',
socket-server_1 | address: '127.0.0.1',
socket-server_1 | port: 6379
socket-server_1 | }
socket-server_1 |
socket-server_1 | Node.js v18.0.0
token2_socket-server_1 exited with code 1```
Try using redis-server as the hostname and not 127.0.0.1.

I get an EADDRINUSE error when trying to start my project with pm2

I get the following error when running 'pm2 start project.json' in my project.
port: 3000 }
0|serv | Tue, 08 Sep 2020 03:14:18 GMT app LoadSettingFromRedis: loaded
0|serv | { Error: listen EADDRINUSE 127.0.0.1:3000
0|serv | at Server.setupListenHandle [as _listen2] (net.js:1360:14)
0|serv | at listenInCluster (net.js:1401:12)
0|serv | at doListen (net.js:1510:7)
0|serv | at _combinedTickCallback (internal/process/next_tick.js:142:11)
0|serv | at process._tickCallback (internal/process/next_tick.js:181:9)
0|serv | errno: 'EADDRINUSE',
0|serv | code: 'EADDRINUSE',
0|serv | syscall: 'listen',
0|serv | address: '127.0.0.1',
0|serv | port: 3000 }
0|serv | Tue, 08 Sep 2020 03:15:08 GMT app LoadSettingFromRedis: loaded
0|serv | Tue, 08 Sep 2020 03:20:43 GMT app LoadSettingFromRedis: loaded
When I check what process is listening on port 3000, I get node. I kill this process, but it still doesn't solve the issue. Does anyone know what is the problem here?
It means your port is already in use. Try killing port with following command
sudo kill -9 $(sudo lsof -t -i:3000)
if that not works try following
sudo lsof -i tcp:3000 // this will return some PIDs
sudo kill -9 [your pid to remove]
Then run pm2 start command again

Issue sending metrics with statsd

I was using the following instructions to install and configure StatsD on a Graphite server:
https://www.digitalocean.com/community/tutorials/how-to-configure-statsd-to-collect-arbitrary-stats-for-graphite-on-ubuntu-14-04
Now that I have a server with StatsD running, I do not see the metrics being logged under /var/log/statsd/statsd.log when I am testing sending them from the command line. Here is what I see:
29 Oct 02:30:39 - server is up
29 Oct 02:47:49 - reading config file: /etc/statsd/localConfig.js
29 Oct 02:47:49 - server is up
29 Oct 14:16:45 - reading config file: /etc/statsd/localConfig.js
29 Oct 14:16:45 - server is up
29 Oct 15:36:47 - reading config file: /etc/statsd/localConfig.js
29 Oct 15:36:47 - DEBUG: Loading server: ./servers/udp
29 Oct 15:36:47 - server is up
29 Oct 15:36:47 - DEBUG: Loading backend: ./backends/graphite
29 Oct 15:36:47 - DEBUG: numStats: 3
The log stays at the last entry of 'numStats: 3', even though I keep entering different metrics at the command line.
Here are a sample of the metrics I entered:
echo "sample.gauge:14|g" | nc -u -w0 127.0.0.1 8125
echo "sample.gauge:10|g" | nc -u -w0 127.0.0.1 8125
echo "sample.count:1|c" | nc -u -w0 127.0.0.1 8125
echo "sample.set:50|s" | nc -u -w0 127.0.0.1 8125
Of interest, I see this under /var/log/statsd/stderr.log:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1039:14)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
at /usr/share/statsd/stats.js:383:16
at null.<anonymous> (/usr/share/statsd/lib/config.js:40:5)
at EventEmitter.emit (events.js:95:17)
at /usr/share/statsd/lib/config.js:20:12
at fs.js:268:14
at Object.oncomplete (fs.js:107:15)
Here is what my localConfig.js file looks like:
{
graphitePort: 2003
, graphiteHost: "localhost"
, port: 8125
, graphite: {
legacyNamespace: false
},
debug: true,
dumpMessages: true
}
Would anybody be able to shed some light as to where the problem lies?
Thanks!
There is a management interface available by default on port 8126: https://github.com/etsy/statsd/blob/master/docs/admin_interface.md
You likely have another service listening on that port in the same system.
Try this:
# localConfig.js
{
graphitePort: 2003
, graphiteHost: "localhost"
, port: 8125
, mgmt_port: 8127
, graphite: {
legacyNamespace: false
},
debug: true,
dumpMessages: true
}
See https://github.com/etsy/statsd/blob/master/exampleConfig.js#L28

Hosting Node with Heroku - Internal Server Error

Im trying to host a Node + Express app with Heroku however when I try to render a view I get an "Internal Server Error"
The connection to Redis works fine.
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.get('/', function(req, res) {
redis.get('foo', function(err, value){
res.render('index', { title: 'My App', Order: value }); -> This Line Fails
console.log(value);
});
});
Loggly Logs:
2011 Nov 14 20:09:42.649 50.19.0.98 126: <40>1 2011-11-14T20:09:42+00:00 d.0bd0cf5e-9521-4fd5-8d4a-b8fcf757e5bf heroku web.1 - - State changed from bouncing to created
2011 Nov 14 20:09:42.669 50.19.0.98 126: <40>1 2011-11-14T20:09:42+00:00 d.0bd0cf5e- 9521-4fd5-8d4a-b8fcf757e5bf heroku web.1 - - State changed from created to starting
2011 Nov 14 20:09:44.531 50.17.63.134 131: <40>1 2011-11-14T20:09:44+00:00 d.0bd0cf5e- 9521-4fd5-8d4a-b8fcf757e5bf heroku web.1 - - Starting process with command `node app.js`
2011 Nov 14 20:09:44.913 50.17.63.134 123: <13>1 2011-11-14T20:09:44+00:00 d.0bd0cf5e-9521-4fd5-8d4a-b8fcf757e5bf app web.1 - - [36minfo -[39m socket.io started
2011 Nov 14 20:09:44.938 50.17.63.134 142: <13>1 2011-11-14T20:09:44+00:00 d.0bd0cf5e-9521-4fd5-8d4a-b8fcf757e5bf app web.1 - - Express server listening on port 27910 in production mode
2011 Nov 14 20:09:45.763 50.19.0.98 121: <40>1 2011-11-14T20:09:45+00:00 d.0bd0cf5e-9521-4fd5-8d4a-b8fcf757e5bf heroku web.1 - - State changed from starting to up
2011 Nov 14 20:09:53.510 184.73.5.216 121: <40>1 2011-11-14T20:09:53+00:00 d.0bd0cf5e-9521-4fd5-8d4a-b8fcf757e5bf heroku api - - Scale to web=1 by ****#gmail.com
2011 Nov 14 20:09:56.027 50.17.63.134 96: <13>1 2011-11-14T20:09:56+00:00 d.0bd0cf5e-9521-4fd5-8d4a-b8fcf757e5bf app web.1 - - 1,2,3,4,5,6
2011 Nov 14 20:09:56.029 50.17.63.134 281: <13>1 2011-11-14T20:09:56+00:00 d.0bd0cf5e- 9521-4fd5-8d4a-b8fcf757e5bf app web.1 - - 10.108.67.105 - - [Mon, 14 Nov 2011 20:09:56 GMT] "GET / HTTP/1.1" 200 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2"
2011 Nov 14 20:09:56.029 50.17.63.134 97: <13>1 2011-11-14T20:09:56+00:00 d.0bd0cf5e-9521-4fd5-8d4a-b8fcf757e5bf app web.1 - - Served Index
2011 Nov 14 20:09:56.144 184.73.5.216 178: <158>1 2011-11-14T20:09:56+00:00 d.0bd0cf5e-9521-4fd5-8d4a-b8fcf757e5bf heroku router - - GET rtcubes.herokuapp.com/ dyno=web.1 queue=0 wait=11ms service=45ms status=200 bytes=21
2011 Nov 14 20:10:05.337 50.17.63.134 281: <13>1 2011-11-14T20:10:05+00:00 d.0bd0cf5e-9521-4fd5-8d4a-b8fcf757e5bf app web.1 - - 10.217.29.248 - - [Mon, 14 Nov 2011 20:10:05 GMT] "GET / HTTP/1.1" 200 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2"
2011 Nov 14 20:10:05.337 50.17.63.134 97: <13>1 2011-11-14T20:10:05+00:00 d.0bd0cf5e-9521-4fd5-8d4a-b8fcf757e5bf app web.1 - - Served Index
2011 Nov 14 20:10:05.337 50.17.63.134 96: <13>1 2011-11-14T20:10:05+00:00 d.0bd0cf5e-9521-4fd5-8d4a-b8fcf757e5bf app web.1 - - 1,2,3,4,5,6
2011 Nov 14 20:10:56.123 50.17.63.134 97: <13>1 2011-11-14T20:10:56+00:00 d.0bd0cf5e-9521-4fd5-8d4a-b8fcf757e5bf app web.1 - - Served Index
2011 Nov 14 20:10:56.125 50.17.63.134 281: <13>1 2011-11-14T20:10:56+00:00 d.0bd0cf5e-9521-4fd5-8d4a-b8fcf757e5bf app web.1 - - 10.217.17.204 - - [Mon, 14 Nov 2011 20:10:56 GMT] "GET / HTTP/1.1" 200 - "-
The way I have fixed the issue was I deployed the app from local machine using Heroku CLI (and not from GitHub as previously done)
I have fixed the issue, Destroyed the app and recreated it on heroku and it just worked. Must have been a configuration problem.
Thanks
I had a similar problem. Figured out I was requesting a wrong page. Fixed.

Resources