jiheielf
jiheielf
CC#
Created by jiheielf on 4/16/2025 in #help
✅ Docker Container Can't connect to remote SQL Server
Currently I have a Worker service that will interact with a sql server on a remote server. when it is deployed to my local machine, it works. But once I deploy it as a docker container, it fails the connection and printout this error
Main loop Exception: Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)
Main loop Exception: Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)
I have tried to search online from stackoverflow and in docker community, I have checked the firewall and tried different combinations compare to their post but it still did not work out...
46 replies
CC#
Created by jiheielf on 3/28/2025 in #help
Worker Service .NET8 how to gracefully shutdown as a docker container
I'm a junior developer just starting to work with Docker, and I have a question regarding graceful shutdown of the worker services. I created a default worker service in Visual Studio, and my Worker class looks something like this:
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
// Do some async work
await myAsyncWork();
await mySecondAsyncWork();
await Task.Delay(1000, stoppingToken);
}
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
// Do some async work
await myAsyncWork();
await mySecondAsyncWork();
await Task.Delay(1000, stoppingToken);
}
}
After publishing the application as a Docker container, I noticed that when I run docker stop, it immediately aborts my async task inside await myAsyncWork(); or await mySecondAsyncWork();. Is there a way to ensure that when I run docker stop, the application finishes the current async task and finishes the current iteration of while (!stoppingToken.IsCancellationRequested) and then gracefully shuts down? And is there any best practise I should follow?
25 replies