Docker Community Forums

Share and learn in the Docker community.

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

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,

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