When I run docker container that hass node express app.js it can not connect to redis-stack-server running locally.
However, if I do it without docker container everything works fine.
I ran esbuild to generate one app.js file but it is express ts app. It connects to redis-stack-server.
Here is my Dockerfile
FROM node:18
ENV REDIS_URL="redis://localhost:6379"
ENV NODE_PORT=3000
COPY app.js .
EXPOSE 3000
CMD [ "node", "app.js" ]
And here is how I set up redis client in node app:
export const redisC = async () => {
const client : RedisClientType = createClient();
client.on('error', (err) => console.error('Redis Client Error', err));
await client.connect();
return client;
}
export let redisClient : RedisClientType;
redisC().then((response) => {
redisClient = response
}).catch(error => console.error(error))
I do run locally:
redis-stack-server
I build docker container:
docker build . -t my-app
Then I run:
docker run -p 3000:3000 my-app
However i face the following issue when I run docker container:
Redis Client Error Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
If I run app.js locally not with docker then everything works.
Express listening on port 3000
And no errors regarding redis connection.
Any help will be very much appreciated. Thanks a lot in advance!
2
Answers
[RESOLUTION] This article really helped! To make it work locally we need to change redis URL to:
So my full redis client config:
Thanks a lot everyone for participation! I really much appreciate the help!
In Docker each container runs in isolation, so
localhost
means different things.localhost
on your machine is not the same aslocalhost
in the express container.Redis is running on
127.0.0.1:6379
on your machine, but not on that address inside the express container. That’s why running express outside of a container works fine.You probably want Docker compose for this.
The crucial part is
REDIS_URL: redis:6379
in the express service, which tells the express container where redis can be contacted, which in Docker compose is using the service name.