I wanna connect my simple expressJs
application with a Redis container. But it’s not connecting with redis
container. Here I’ve used redis:alpine image to build the container.
kasun@Kasuns-MacBook-Air ~ % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis alpine 9dcd83a87127 3 weeks ago 36.5MB
alpine latest 3fb3c9af89a9 7 weeks ago 5.32MB
kasun@Kasuns-MacBook-Air ~ %
I’ve run a container like this way and bounded the port 3307 of the local machine
kasun@Kasuns-MacBook-Air ~ % docker run -dt --name redis_cache -p3307:6379 9dcd83a87127
8f44169e8c73938845319463c83f63048c5051bcbbfca7809a1300446b415ae3
kasun@Kasuns-MacBook-Air ~ %
It shows the container running like this
kasun@Kasuns-MacBook-Air ~ % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8f44169e8c73 9dcd83a87127 "docker-entrypoint.s…" 56 seconds ago Up 55 seconds 0.0.0.0:3307->6379/tcp redis_cache
kasun@Kasuns-MacBook-Air ~ %
expressJs code (server.js file)
const express = require('express');
const app = express();
const redis = require('redis');
const redisClient = redis.createClient(3307, '0.0.0.0');
redisClient.on('connect', () => {
console.log("Redis connected");
})
redisClient.on('error', () => {
console.log("Error");
})
app.listen(8080, () => {
console.log('listening on port 8080');
})
But when I start express application, it does not connect with the Redis container.
kasun@Kasuns-MacBook-Air test % npm run test
> [email protected] test
> nodemon server.js
[nodemon] 2.0.16
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
listening on port 8080
How can I solve this problem?
2
Answers
Change 0.0.0.0 to localhost or 127.0.0.1
try this way
this code is worked with me, you can try this