I have tried dockerization asp.net 6, redis and postgresql with docker compose and I get the following error.
The timeout was reached before the message could be written to the output buffer, and it was not sent, command=PING, timeout: 5000, inst: 0, qu: 0, qs: 0, aw: False, bw: CheckingForTimeout, serverEndpoint: redis_image:6380, mc: 1/1/0, mgr: 10 of 10 available, clientName: 127d47c5ce33(SE.Redis-v2.5.61.22961), IOCP: (Busy=0,Free=1000,Min=12,Max=1000), WORKER: (Busy=0,Free=32767,Min=12,Max=32767), POOL: (Threads=12,QueuedItems=0,CompletedItems=1665), v: 2.5.61.22961 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)
My Dockerfile for Asp.Net 6
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 5000
ENV ASPNETCORE_URLS=http:// :5000
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MediatrExample.API/MediatrExample.API.csproj", "MediatrExample.API/"]
COPY ["MediatrExample.CQRS/MediatrExample.CQRS.csproj", "MediatrExample.CQRS/"]
COPY ["MediatrExample.Service/MediatrExample.Service.csproj", "MediatrExample.Service/"]
COPY ["MediatrExample.Data/MediatrExample.Data.csproj", "MediatrExample.Data/"]
COPY ["MediatrExample.Core/MediatrExample.Core.csproj", "MediatrExample.Core/"]
COPY ["MediatrExample.Shared/MediatrExample.Shared.csproj", "MediatrExample.Shared/"]
RUN dotnet restore "MediatrExample.API/MediatrExample.API.csproj"
COPY . .
WORKDIR "/src/MediatrExample.API"
RUN dotnet build "MediatrExample.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MediatrExample.API.csproj" -c Release -o /app/publish
FROM base AS final
EXPOSE 5000
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MediatrExample.API.dll"]
My Docker Compose yml file
version: '3.4'
networks:
mediatrexample:
driver: bridge
services:
pgsql_image:
image: postgres:latest
container_name: postgersql_db
restart: always
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "123456"
POSTGRES_DB: "ecommerce"
ports:
- 5432:5432
networks:
- mediatrexample
redis_image:
image: 'redis:latest'
container_name: redis_db
restart: always
environment:
- REDIS_PASSWORD=RRnFPZ93tjBHB9W62p
- REDIS_PORT=6380
ports:
- '6380:6379'
command: [ "redis-server" ]
networks:
- mediatrexample
mediatrexample.api:
image: ${DOCKER_REGISTRY-}mediatrexampleapi
container_name: mediatr_example
depends_on:
- pgsql_image
- redis_image
build:
context: .
dockerfile: MediatrExample.API/Dockerfile
networks:
- mediatrexample
links:
- pgsql_image
- redis_image
environment:
- CONNECTIONSTRINGS__NpgSQLConnection=User ID=postgres;Password=123456;Host=pgsql_image;Port=5432;Database=ecommerce
- Redis__Host=redis_image
- Redis__Port=6380
- Redis__Password=RRnFPZ93tjBHB9W62p
My ConnectionMultiplexer.Connect Code is in Program.cs
var multiplexer = RedisUtility.ConnectRedis(Configuration);
builder.Services.AddSingleton<IConnectionMultiplexer>(multiplexer);
My RedisUtility.ConnectRedis Code is
public static class RedisUtility
{
public static ConnectionMultiplexer ConnectRedis(IConfiguration Configuration)
{
var redisOptions = ConfigurationOptions.Parse($"{Configuration["Redis:Host"]}:{Configuration["Redis:Port"]}");
redisOptions.Password = Configuration["Redis:Password"];
redisOptions.AbortOnConnectFail = false;
var multiplexer = ConnectionMultiplexer.Connect(redisOptions);
return multiplexer;
}
}
My Redis Service Class is and Lifecycle is Singleton
public class RedisCacheService : IRedisCacheService
{
private IDatabase _database;
private static IConnectionMultiplexer _connectionMultiplexer;
public RedisCacheService(IConnectionMultiplexer connectionMultiplexer)
{
_connectionMultiplexer = connectionMultiplexer ?? throw new ArgumentNullException(nameof(connectionMultiplexer));
_database = _connectionMultiplexer.GetDatabase(0);
}
public async Task<T> GetAsync<T>(string key)
{
return default;
}
public async Task RemoveAsync(string key)
{
throw new NotImplementedException();
}
public async Task SetAsync<T>(string key, T value, int expiredInMinute = 120)
{
//RedisKey key1 = new RedisKey(key);
//var valueStr = JsonSerializer.Serialize(value);
//RedisValue redisValue = new RedisValue(valueStr);
//await _database.StringSetAsync(key1, redisValue, TimeSpan.FromMinutes(expiredInMinute));
var pong = await _database.PingAsync();
}
}
I have tried many solutions such as SSL=True, Tls12, Different Version Stackexchange.Redis, but I cant find why stackexchange.redis doesn't connect redis. I tried to connect redis with Another Redis Desktop Manager app and its success connect.
CodePudding user response:
I think the issue is with port
- Redis__Port=6380
when you are in docker network you should use internal port, not the one which is exposed for outside. it should be
- Redis__Port=6379