Connecting Redis from my network in docker .net Core application

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

1 Like

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.

1 Like

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ā€]

Docker Compose file
version: ā€˜3.4ā€™
services:
apimodule:
image: apimodule
build:
context: .
dockerfile: Dockerfile

Redis Container

redis container is running on 172.17.0.2:6379

first i have to create container and then run interactive?

can you give me command to link redis container with my container,

1 Like

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")

i have not added redis in docker compose, do i have to add?
i have sent what ever i have.

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:

version: '3.4'
services:
  apimodule:
    # ...
  redis:
    image: redis

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.

1 Like

thanks for response.

yes i am using linux container

Docker compose file
version: ā€œ3.4ā€
services:
coreapidemo:
image: coreapidemo
build:
context: .
dockerfile: Dockerfile
ports:
- ā€œ5000:80ā€
redis:
image: redis
ports:
- ā€œ6379:6379ā€

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

version: '3.4'

services:

  apimodule:

    # ...

  links:

    - redis 

  redis:

    image: redis

    port: 

        - 6379:6379

     volumes:

        - /path/to/redis.conf:/tmp/redis.conf

    command: [ "redis-server", "/tmp/redis.conf"]

Now in the ConnectionMultiplexer.Connect(ā€œredis:6379ā€) will connect to the redis container.

1 Like

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)

My docker-compose.yml:

version: ā€˜3.4ā€™
services:
cycler:
build: ./
ports:
- ā€œ8080:8080ā€
depends_on:
- redis
restart:
ā€œalwaysā€
redis:
image: redis
ports:
- 6379:6379
volumes:
- ./redis.conf:/tmp/redis.conf
command: [ ā€œredis-serverā€, ā€œ/tmp/redis.confā€]

redis.conf:

#bind 127.0.0.1
bind 0.0.0.0

Connection code:

const queue = new Queue(ā€˜queueā€™, {
redis: {
host: ā€˜redisā€™,
port: 6379
}
})
None of the solutions provided here, here https://github.com/luin/ioredis/issues/763 and here http://progressivecoder.com/docker-compose-nodejs-application-with-redis-integration/ solved my problem. :frowning:

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.

Thanks for your reply. I was able to resolve this.

Hi, how did you solve the problem, Iā€™ve been suffering for the second day (
I am using gitlab-ci