R
Railway16mo ago
tansan

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
Percy16mo ago
Project ID: N/A
tansan
tansanOP16mo ago
049b9d61-9cd8-4102-aad9-1350a56e2b7e
tansan
tansanOP16mo ago
I saw this message, but unclear what the actionable item is here for my Dockerfile
Brody
Brody16mo ago
send the dockerfile please
tansan
tansanOP16mo ago
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
Brody16mo ago
okay so help me understand why you have redisport stuff?
tansan
tansanOP16mo ago
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
Brody16mo 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
tansanOP16mo ago
tansan
tansanOP16mo ago
Removed the ARG and EXPOSE and still unable to connect
Brody
Brody16mo ago
I know, we will work through this, have patience please
tansan
tansanOP16mo ago
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
Brody16mo ago
go ahead and add the rest of the redis reference variables now
tansan
tansanOP16mo ago
Okay, I just finished doing that
Brody
Brody16mo ago
screenshot please
tansan
tansanOP16mo ago
tansan
tansanOP16mo ago
Do I have to keep those env vars even if I am only referencing REDIS_URL in my application code?
Brody
Brody16mo 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
tansan
tansanOP16mo ago
Brody
Brody16mo ago
what package are you using for redis
tansan
tansanOP16mo ago
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
Brody16mo ago
what's the ! for
tansan
tansanOP16mo ago
TypeScript thing to tell it the var will always be defined
Brody
Brody16mo ago
fancy
tansan
tansanOP16mo ago
yeah pretty useful
Brody
Brody16mo 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
tansanOP16mo ago
Yeah, I suspect it might be a docker issue
Brody
Brody16mo 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
tansanOP16mo ago
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
Brody16mo ago
unfortunately "it works locally" does not rule out this being a code issue
Solution
tansan
tansan16mo ago
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
tansanOP16mo ago
Okay, I'll sort it out. I'll try explicit env vars of port, user, pass, etc...
Brody
Brody16mo ago
see, it's a good thing I had you add them all!
tansan
tansanOP16mo ago
kekW
Brody
Brody16mo ago
foresight and see, it's not a docker issue
tansan
tansanOP16mo ago
Not a docker issue, but definitely relevant
Brody
Brody16mo ago
I don't see how it is, but lol
tansan
tansanOP16mo ago
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
Brody16mo ago
it's just a loopback address, really nothing to do with docker
tansan
tansanOP16mo ago
@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
Brody16mo 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
tansanOP16mo ago
My apologies. I appreciate you letting me know.
Brody
Brody16mo 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
tansanOP16mo ago
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
Brody16mo ago
no problem
Want results from more Discord servers?
Add your server