1

I have a C# program which I want to connect to a simple Dockerized SQL Server database on Windows. The docker-compose.yml for the database looks like this:

version: "3.9"
services:
    db:
        image: "mcr.microsoft.com/mssql/server"
        environment:
            SA_PASSWORD: "*****"
            ACCEPT_EULA: "Y"

Now I am trying to connect to it in C# like this:

SqlConnection connection = new SqlConnection(@"Server=db;Database=master;User=sa;Password=*****;");
connection.Open();

At connection.Open() it fails with this error:

System.Data.SqlClient.SqlException: [...]
System.ComponentModel.Win32Exception: The Network Path could not be found

It seems obvious to me that the problem is the connection string. However, I have searched and searched and wasn't able to figure out what the correct connection string in this case would be. Can someone please help me out?


EDIT:

I now also tried this connection string:

@"Server=localhost,1433;Database=master;User=sa;Password=*****;"

This result in the following, different error:

System.ComponentModel.Win32Exception: The Remote Host refused the network connection

6
  • Are you sure the server variable of "db" would actually map to anything? I would suspect a real server name or localhost or something. Commented Jul 28, 2021 at 17:07
  • Use SQL Server Management Studio to connect to to the Server. The login window of SSMS has the Server/Instance that you should be using in your connection string. Commented Jul 28, 2021 at 17:08
  • Is your database on a bridge (the default) network? Is your C# app on the same network? Commented Jul 28, 2021 at 17:11
  • @HansKilian ; I honestly don't know. The above docker-compose.yml is literally the complete file, and I don't know anything about bridges and networks, which is why I'm asking what I need to do to get this to work. Commented Jul 28, 2021 at 17:15
  • @jdweng : I can't connect to the server via SQLSMS either. There the same errors occur. Commented Jul 28, 2021 at 17:24

1 Answer 1

1

I now figured out what the problem was.

The problem was that my docker-compose.yml was still missing the ports section. For some reason the tutorial from which I copied this didn't feature this section.

So here is how the docker-compose.yml needs to look:

version: "3.9"
services:
    db:
        image: "mcr.microsoft.com/mssql/server"
        environment:
            SA_PASSWORD: "*****"
            ACCEPT_EULA: "Y"
        ports:
            - "1433:1433"

After that, I am able to connect to the database from C# using the following connection string:

@"Server=localhost,1433;Database=master;User=sa;Password=*****;"
Sign up to request clarification or add additional context in comments.

5 Comments

Port 1433 is the default port number and is not needed. Using localhost alone would of solved issue. SqlConnection uses a driver that will automatically use port 1433.
@jdweng The "ports" parameter maps the host's port to the container's one. Without that, you wouldn't be able to reach the database within the container when sending requests to localhost
@m_ocean : Do you know what default means? When SQL is installed the default port is 1433 and drivers default to port 1433 unless the user specifies a different port.
@jdweng Yeah, but you don't account for that it is running inside a Docker container here. By default, you can't access services inside Docker containers by talking to localhost. For that, you need port mapping — a feature that Docker provides.
@jdweng Oh, I see... We're talking about different things, my bad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.