Hi,
I have created a console application in .net core to publish and subscribe message in Redis.
I have given connection string 127.0.0.1:6379, localhost:6379 when I run locally in my system it works.
but when I create docker image and run in docker it gives me an error
in docker image i have used only to publish code and entry point to dotnet command.
and not exposing any port.
StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s). UnableToConnect on localhost:6379/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 0s ago, last-write: 0s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 0s ago, v: 2.1.30.3889
i tried to search in google , but did not get any useful information
You need to place both containers in the same network and connect to Redis using its container name. If youāre using docker run commands, you need to create your own network. With Docker Compose, the network is created for you.
If you paste your docker run commands or your docker-compose.yml file weāll be able to show you how to make that happen.
If the containers already share a network thatās not the default network, you can simply change the connection string in your .net application. If your container is named redis , replace localhost:6379 with redis:6379.
thanks for quick response, you are faster than google !!!
sorry for such silly question , but i am beginner in thatā¦
Docker File
FROM mcr.microsoft.com/dotnet/core/runtime:3.1 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY [āAPIModule.csprojā, ā./ā]
RUN dotnet restore ā./APIModule.csprojā
COPY . .
WORKDIR ā/src/.ā
RUN dotnet build āAPIModule.csprojā -c Release -o /app/build
FROM build AS publish
RUN dotnet publish āAPIModule.csprojā -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT [ādotnetā, āAPIModule.dllā]
You havenāt posted your entire docker-compose.yml, I canāt see your redis service.
In any case, what you have to do is change the connection string in your .net app from localhost:6379 to redis:6379 (if redis is the name of your redis container). Look inside StackExchange.Redis, something in the lines of ConnectionMultiplexer.Connect(...). Should be ConnectionMultiplexer.Connect("redis:6379")
Itās best to run your entire application stack in containers, so that includes Redis. Itāll make connecting the containers with each other easier. You can add the following:
Then connect to Redis like Iāve told you in the previous post. Donāt use localhost:6379 or 127.0.0.1:6379, that wonāt work.
Otherwise, you could connect from your app container to Redis thatās running on your localhost, youāll have to use host.internal.docker:6379. Last I checked it didnāt work on Linux though, they were still fixing it.
what i did i added network using docker create network command
name = mynet
then
i run redis in mynet network
then i run my application container in mynet network
docker run -it --rm apimodule --network=mynet
docker run -it --rm redis --network=mynet
then i give connection string redis:6379
but still same error,
StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s). UnableToConnect on redis:6379/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 0s ago, last-write: 0s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 4s ago,
v: 2.1.30.38891
A possible reason could be that itās trying to connect to Redis while itās still being started. If you add depends_on https://docs.docker.com/compose/compose-file/#depends_on to your coreapidemo service, then docker compose will start Redis first.
Did you also make sure Interactive db in Redis exists?
i have faced the same issue too. This is because the redis container is listening on 127.0.0.1
So in the redis.conf file comment out this binding and add bind 0.0.0.0
Your redis.conf file will look like this
# bind 127.0.0.1
bind 0.0.0.0
This file needs to be passed as an argument while starting the redis container
Hi, I have the same issue, but Iām working with NodeJS.
It throws:
cycler_1 | (node:19) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6379
cycler_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
cycler_1 | (node:19) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
I tried the below :
1: Created a container for Redis named āredisā used the same in connection string : āredis:5003ā
I still get the error cannot connect to server.
I am running my application from my IDE, if i use local host instead of container name it works.
If I try deploying containers using docker compose the redis container gets a different name so the name i have provided as container name in the connection string in .net app still throws cannot connect to server
How did you set the container name? When you use docker compose, the name of the service is not the name of the container. You can set the container name using container_name, but the service name should also be resolved to the containerās IP address.