Cannot connect to SQL server from a docker container with .Net 6

Hela Masri 25 Reputation points

I have an .NET 6 application running in docker and I want to connect to an SQL Server database, but I'm unable to do so. The .Net application works fine when I run it simply but when I run the dockerfile or a docker compose I have an error.

The connection string is the following, using a named instance
connectionStringSQL = $"Data Source={sql_data_source}; Initial Catalog = {sql_initial_catalog}; User Id = {sql_user_id}; password = {sql_password}; Encrypt = False; TrustServerCertificate = True";

The dockerfile is as follows:

FROM AS base

FROM AS build
COPY ["CronJob/CronJob.csproj", "CronJob/"]
RUN dotnet restore "CronJob/CronJob.csproj"
COPY . .
WORKDIR "/src/CronJob"
RUN dotnet build "CronJob.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "CronJob.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CronJob.dll"]

Only the named instance version (databaseXX\YY) works when I run the c# code. And for the docker run I have the following error when using the named version

Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started. at Microsoft.Data.SqlClient.SNI.SSRP.GetPortByInstanceName

when I add the port number (databaseXX\YY, Port) I have the following error
Microsoft.Data.SqlClient.SqlExceptionSystem.AggregateExceptionSystem.Net.Internals.SocketExceptionFactory+ExtendedSocketException", "message": "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.

When I set the Data Source to this format IPAddress,Port

I'm not able to connect either from docker image nor from regular run of .Net application

A set of technologies in the .NET Framework for building web applications and XML web services.
4,142 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,655 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Seeya Xi-MSFT 16,436 Reputation points

    Hi @Hela Masri,

    There are a few things you can try to resolve this issue:

    1. Check if the SQL Server Browser service is running. The SQL Server Browser service listens for incoming requests for Microsoft SQL Server resources and provides information about SQL Server instances installed on the computer.
    2. Make sure that TCP/IP connections are enabled for your SQL Server instance and make sure that TCP/IP is enabled. Both of these you can check from the SQL Server Configuration Manager.
    3. Check your firewall settings. Make sure that the SQL Server port is open on the firewall. The default port for SQL Server is 1433, but this can be configured during installation.
    4. Verify the connection string in your .NET application. Make sure that the Data Source value is correct and includes the correct port number. You can also try using the IP address of the SQL Server instead of the server name.
    5. If you're running your .NET application in a Docker container, make sure that the container is connected to the correct network and can communicate with the SQL Server instance. You can also try exposing the SQL Server port on the host machine and mapping it to the container port using the -p option when running the Docker container.

    Best regards,


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".