71

I'm using Visual Studio 2017 for mac with dotnet Core and EF Core. After setting up the mssql image in Docker container , I was trying to add the connection string but throwing connection error. I tried with different options such as ip address , container name , host name etc. as server name but none of them worked.

 "Default": "Server=172.17.0.2; Database=ERPDb; User=sa; Password =******;"

with container name

 "Default": "Server=ecstatic_hermann; Database=ERPDb; User=sa; Password=******;"

with hostname :

 "Default": "Server=f45840a59623; Database=ERPDb; User=sa; Password=******;"

While connecting through using localhost in Terminal its successfully connecting

$ mssql -s localhost -p Technocrat123
Connecting to localhost...done

sql-cli version 0.6.2
Enter ".help" for usage hints.

But when running the application the connection fails.

Appreciate any help. Thanks in advance.

If using localhost then error is

Login failed for user ''. Reason: An attempt to login using SQL authentication failed. Server is configured for Integrated authentication only.
19
  • is the sql server running inside a container ? Is the connecting code also running inside a container? If yes to both, are they on the same network, and if not, is the server's SQL port exposed?
    – omu_negru
    Commented Aug 16, 2017 at 11:38
  • @omu_negru The sql server is running inside the container. The application is running inside the machine not inside the container. How to expose the SQL port? Can you please guide me? Commented Aug 16, 2017 at 11:48
  • expose the MySql port by passing the -p param to the docker run command: docker run -p 3306:3306 ....... Also set the host to localhost, then you can check if the port is exposed by running docker ps and checking the ports section on the right.
    – omu_negru
    Commented Aug 16, 2017 at 11:50
  • @omu_negru I have already done that docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Technocrat123' -p 1433:1433 -d microsoft/mssql-server-linux Commented Aug 16, 2017 at 11:51
  • Is port 1433 the one exposed by the DB server? I thought the normal MySQL port was 3306
    – omu_negru
    Commented Aug 16, 2017 at 11:53

6 Answers 6

74
sudo docker pull microsoft/mssql-server-linux:2017-latest
docker run \
  -e 'ACCEPT_EULA=Y' \
  -e 'MSSQL_SA_PASSWORD=YourSTRONG!Passw0rd' \
  -p 1401:1433 \
  -n sql1 \
  -d microsoft/mssql-server-linux:2017-latest

then,

private static string _connStr = @"
  Server=127.0.0.1,1401;
  Database=Master;
  User Id=SA;
  Password=YourSTRONG!Passw0rd";
4
  • 5
    Is there a way to connect using the server=(LocalDb)\InstanceName format?
    – amorimluc
    Commented Dec 12, 2018 at 17:33
  • Can you provide any more information on your setup? Why are you using named instances?
    – rjminchuk
    Commented Dec 13, 2018 at 19:10
  • Because our software team is mostly using Windows, but I'm on a mac. They are connecting using the format I referenced. I'm having trouble connecting that way to my docker SQL Server container.
    – amorimluc
    Commented Dec 13, 2018 at 21:06
  • I'm not sure it is possible from the command line on MacOS to connect with the servername\instance format. I've tried a myriad of different ways to connect based on the @@servername and @@servicename (instance name) on MacOS with no success. I keep getting network errors.
    – rjminchuk
    Commented Dec 16, 2018 at 23:25
30

Rather than use IP addresses, which would be problematic in a team environment, you can also use host.docker.internal which will resolve to your host IP.

Data Source=host.docker.internal,1433;Initial Catalog=MyDB;User ID=MyUser;Password=MyPassword
1
  • I believe this is only true for Linux containers (this stung me before when I forced onto windows containers. So be careful here) Commented Jan 2, 2023 at 17:48
26

Most likely your server name is localhost and port 1401 (which is the default for Docker container setup). Therefore, you'll need to use the following connection string:

"Default": "Server=localhost,1401; Database=ERPDb; User=sa; Password =******;"
8

I had this problem today and I resolved it using a separate network (instead of using default "bridge" network).

  1. docker network create test_network

  2. docker container run -p 1433:1433 -d --name mssql -v mssql_data:/var/opt/mssql -e SA_PASSWORD=********** -e ACCEPT_EULA=Y --network=test_network microsoft/mssql-server-linux

  3. docker container run -p 5000:80 --rm -e ASPNETCORE_ENVIRONMENT=Development --name aspnetcore --network=test_network aspnetcore-image

Also I have such connection string:

Server=mssql;Database=master;User=sa;Password=**********;

About previous answers regarding Connection String with IP address, it is not a good approach, because this address can be changed dynamically, it is better to use container names, as hostnames.

1
  • 1
    IP 127.0.0.1 never changes. It is a self reference just like localhost. Commented Nov 12, 2020 at 20:53
2

In my case what worked was using jsut localhost but adding TrustServerCertificate=True

 "Server=localhost;Database=ERPDb;User Id=SA;Password=YourSTRONG!Passw0rd;TrustServerCertificate=True"
1

I solved it by using SQL server Container IP address by inspecting the sql server container for its IP address as shown below

docker inspect -f "{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}" <ContainerID | ContainerName>
1
  • This IP address doesn't work in most environments (including MacOS and Windows hosts) and you shouldn't need to look it up.
    – David Maze
    Commented Mar 15, 2022 at 10:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.