im trying create a demo in asp.net core 8 to save data in redis cache. Everything work perfect when i use localhost:6379. But when my app.net container its runiing and try to comunicate with container redis, by using container name ex: http://redis:6379, i receive the messge:
“UnableToConnect on 127.0.0.1:6379/Interactive, Initializing/NotStarted, “
here my conpose:
api:
image: api:latest
container_name: api
build:
context: ../src/api
dockerfile: ./Dockerfile
ports:
- "5168:5168"
environment:
- aspnetcore_environment=Production
- aspnetcore_urls=http://+:5168
redis:
image: redis:latest
container_name: redis
restart: always
ports:
- "6379:6379"
volumes:
- ./volumes/redis.data:/usr/share/root/redis
- ./volumes/redis.conf:/usr/share/redis/redis.conf
environment:
- REDIS_PASSWORD=Teste@123
- REDIS_PORT=6379
- REDIS_DATABASES=16
command: [ "redis-server", "/usr/share/redis/redis.conf"]
And here my progam.cs:
var redisOptions = new ConfigurationOptions
{
EndPoints = { { appServicesSettings.RedisHost, appServicesSettings.RedisPort } },
AbortOnConnectFail = false,
DefaultDatabase = 0,
ConnectTimeout = 15000,
SyncTimeout = 15000
};
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = appServicesSettings.Redis;
options.InstanceName = "SampleInstance";
});
builder.Services.AddSingleton<IConnectionMultiplexer>(ConnectionMultiplexer.Connect(redisOptions));
appsetting:
"AppServicesSettings": {
"RedisLocal": "localhost:6379",
"RedisHost": "http://redis",
"RedisPort": 6379
}
version:
Microsoft.Extensions.Caching.StackExchangeRedis” Version=”8.0.4″