Can't connect to Redis

This is the error I am getting. What I have checked so far * Made sure I used the env variables provided by Railway * Exposed the REDISPORT in my dockerfile
Error: connect ECONNREFUSED 127.0.0.1:6379

at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {

errno: -111,

code: 'ECONNREFUSED',

syscall: 'connect',

address: '127.0.0.1',

port: 6379

}
Error: connect ECONNREFUSED 127.0.0.1:6379

at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {

errno: -111,

code: 'ECONNREFUSED',

syscall: 'connect',

address: '127.0.0.1',

port: 6379

}
Solution:
GitHub
createClient trying to connect with localhost even though URL is fo...
Description I am trying to create a client with URL redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}, and sending REDIS_HOST=rediss and REDIS_PORT=6379, but still it is trying to connect...
Jump to solution
45 Replies
Percy
Percy10mo ago
Project ID: N/A
tansan [chill/build mode]
049b9d61-9cd8-4102-aad9-1350a56e2b7e
tansan [chill/build mode]
I saw this message, but unclear what the actionable item is here for my Dockerfile
Brody
Brody10mo ago
send the dockerfile please
tansan [chill/build mode]
FROM node:20-alpine

WORKDIR /app

ARG REDISPORT=6379

RUN apk add --update --no-cache \
make \
g++ \
jpeg-dev \
cairo-dev \
giflib-dev \
pango-dev \
libtool \
autoconf \
automake \
git

COPY package.json ./
COPY package-lock.json ./

RUN npm ci

COPY . .

# redis
EXPOSE ${REDISPORT}/tcp

CMD ["npm", "run", "start"]
FROM node:20-alpine

WORKDIR /app

ARG REDISPORT=6379

RUN apk add --update --no-cache \
make \
g++ \
jpeg-dev \
cairo-dev \
giflib-dev \
pango-dev \
libtool \
autoconf \
automake \
git

COPY package.json ./
COPY package-lock.json ./

RUN npm ci

COPY . .

# redis
EXPOSE ${REDISPORT}/tcp

CMD ["npm", "run", "start"]
I believe that is correct?
Brody
Brody10mo ago
okay so help me understand why you have redisport stuff?
tansan [chill/build mode]
I thought Docker was not exposing the port correctly and the one provided to me by Railway is different from the default, so I am accepting it via the env var and exposing it
Brody
Brody10mo ago
you're connecting to redis, you don't need to expose anything remove the ARG and EXPOSE stuff please next, show me a screenshot of your service variables please
tansan [chill/build mode]
Removed the ARG and EXPOSE and still unable to connect
Brody
Brody10mo ago
I know, we will work through this, have patience please
tansan [chill/build mode]
Oh I was just updating you since you were instructing me to remove the arg and expose. I wasn't trying to rush you. My apologies if it came off that way.
Brody
Brody10mo ago
go ahead and add the rest of the redis reference variables now
tansan [chill/build mode]
Okay, I just finished doing that
Brody
Brody10mo ago
screenshot please
tansan [chill/build mode]
Do I have to keep those env vars even if I am only referencing REDIS_URL in my application code?
Brody
Brody10mo ago
no, but I don't know what you reference in code so it was a safe bet to ask you to use them all now show me the redis connector code please
Brody
Brody10mo ago
what package are you using for redis
tansan [chill/build mode]
GitHub
GitHub - redis/node-redis: Redis Node.js client
Redis Node.js client. Contribute to redis/node-redis development by creating an account on GitHub.
Brody
Brody10mo ago
what's the ! for
tansan [chill/build mode]
TypeScript thing to tell it the var will always be defined
Brody
Brody10mo ago
fancy
tansan [chill/build mode]
yeah pretty useful
Brody
Brody10mo ago
okay well it looks fine then but your error comes from trying to connect to the default redis host and port, so your code isn't actually trying to connect to the url you have specified
tansan [chill/build mode]
Yeah, I suspect it might be a docker issue
Brody
Brody10mo ago
no it's not, this is a code issue share your repo and I'll have a look to see if anything stands out for me?
tansan [chill/build mode]
For what it's worth, I have it working locally with a redis url I can share the file, but let me double check one thing
Brody
Brody10mo ago
unfortunately "it works locally" does not rule out this being a code issue
Solution
tansan [chill/build mode]
GitHub
createClient trying to connect with localhost even though URL is fo...
Description I am trying to create a client with URL redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}, and sending REDIS_HOST=rediss and REDIS_PORT=6379, but still it is trying to connect...
tansan [chill/build mode]
Okay, I'll sort it out. I'll try explicit env vars of port, user, pass, etc...
Brody
Brody10mo ago
see, it's a good thing I had you add them all!
Brody
Brody10mo ago
foresight and see, it's not a docker issue
tansan [chill/build mode]
Not a docker issue, but definitely relevant
Brody
Brody10mo ago
I don't see how it is, but lol
tansan [chill/build mode]
y u being like this naw but i think you're right though. I always think its docker related when i see errors related to 127.0.0.1
Brody
Brody10mo ago
it's just a loopback address, really nothing to do with docker
tansan [chill/build mode]
@Brody guess what. It's docker relevant. Wait going to share a repo with you. I think you are more knowledgable here and might be able to explain this better than I can. @Brody Is it possible to rotate my REDIS username and password later? I am thinking to just hardcode it in the private repo for the brevity
Brody
Brody10mo ago
chill with the pings please #🛂|readme #5 but yes you can rotate your redis credentials, and no you should not hardcode the credentials even for brevity
tansan [chill/build mode]
My apologies. I appreciate you letting me know.
Brody
Brody10mo ago
just share your repo lol again, it's not a docker issue, if this was, there would be a hell of a lot more people in here saying they can't connect to redis, since regardless of using a dockerfile or nixpacks, docker is used in the end
tansan [chill/build mode]
Ah nevermind. I was testing the repo then realized I made mistake in my earlier testing. I can't figure out the issue. Will try again in the morning! However, it's clear its a library issue, so I don't want to use anymore of your time! Thank you for the help, as always!
Brody
Brody10mo ago
no problem