'.net core 6 Failed to load resource: net::ERR_CONNECTION_REFUSED

I am using both postman and swagger to test the endpoint of my API. I have connected to a postgres docker container image for my database running on docker desktop with a run command :

docker run -d -p 5432:5432 --name {name} -e POSTGRES_PASSWORD={password} postgres

and am able run migrations on the container to create the database but cannot pull/ query data from the db and run into an error after enabling CORS in the Program.cs file. I have also added a dev cert to my application. I cannot figure out if it is docker networking related because I am a beginner when it comes to docker, or if it is CORS related and I have missed something :

Program.cs File:

builder.Services.AddCors(options =>

options.AddPolicy("AllowCorsPolicy", policyBuilder => policyBuilder
    .AllowCredentials()
    .AllowAnyHeader()
    .AllowAnyMethod()
    .WithOrigins("https://localhost:5001")
));

builder.Host.ConfigureLogging(logging =>
{
 logging.ClearProviders();
 logging.AddConsole();
});

builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();

builder.Services.AddSwaggerGen();

builder.Services.AddDbContext<GrowDbContext>(
options =>
 options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"));

builder.Services.AddScoped<IGrowRepository, GrowRepository>();
builder.Services.AddScoped<IGrowService, GrowService>();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
   app.UseSwagger();
   app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseRouting();
app.UseCors("AllowCorsPolicy");

app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
  endpoints.MapControllers();
});

app.Run("https://localhost:5001");

appsettings.json file :

{
  "ConnectionStrings": {
     "DefaultConnection": "Host=localhost;Port=5432;Database=growDb;Username= 
      {username};Password={password};"
},
 "Logging": {
   "LogLevel": {
     "Default": "Information",
     "Microsoft.AspNetCore": "Warning",
     "Microsoft.Hosting.Lifetime": "Information"
  }
},
 "AllowedHosts": "*"

Error message : Error message of http get request



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source