I’m having trouble getting my ASP.NET Core 8 application to connect to SQL Server when it runs inside a Docker container. This is my first experience with Docker and .NET together and I’m really struggling.
I have this database utility class that handles migrations:
public static class DatabaseManager
{
public static async Task HandleDatabaseSetupAsync(IServiceProvider provider, CancellationToken token)
{
using var context = provider.GetRequiredService<ApplicationDbContext>();
var outstandingMigrations = await context.Database.GetPendingMigrationsAsync(token);
if (outstandingMigrations.Any())
{
await context.Database.MigrateAsync(token);
}
}
}
The error happens when it tries to check for pending migrations. I get this SQL Server connection error about network issues and the server not being found.
I’ve tried enabling different protocols in SQL Server Configuration Manager and restarting services. I’ve also experimented with various connection string formats like using IP addresses and different instance names but nothing seems to work.
Has anyone successfully connected to a local SQL Server instance from within a Docker container? What am I missing here?