connection string for sqlserver in Docker container
Asked Answered
U

6

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.
Understanding answered 16/8, 2017 at 11:17 Comment(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?Forefather
@Forefather 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?Understanding
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.Forefather
@Forefather I have already done that docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Technocrat123' -p 1433:1433 -d microsoft/mssql-server-linuxUnderstanding
Is port 1433 the one exposed by the DB server? I thought the normal MySQL port was 3306Forefather
@Forefather docker ps -> 56e88580a807 microsoft/mssql-server-linux "/bin/sh -c /opt/m..." 2 hours ago Up 2 hours 0.0.0.0:1433->1433/tcp ecstatic_hermannUnderstanding
@Forefather 1433 was exposed by DB server . got from their documentationUnderstanding
are the services running on different machines? If yes, can you ping the DB host from the code host?Forefather
@Forefather visual studio and the docker is in the same machine. how to ping? you mean try connecting like connect to db option?Understanding
in that case try setting the server ip to 127.0.0.1, or localhostForefather
@Forefather I tried using 127.0.0.1 and localhost , didnt work . Also, not able to explore the DBs since there is no object explorer to directly connect to DB in VS mac version.Understanding
@Forefather when I tried localhost it throws error Login Failed for userUnderstanding
@Understanding did you find the solution? I'm having the same problem.Reiter
@BrunoSerrano Unfortunately no.Understanding
@BrunoSerrano Appreciate if you can share in case you find a fix for it.Understanding
Here is the connection string that worked for me: Data Source=localhost;Initial Catalog=<database>;User ID=sa;Password=<password>Reiter
@BrunoSerrano thanks for your message. I had initially started with localhost , but was not successful . Appreciate you shared it here.Understanding
@Understanding have you tried with Data Source instead of Server and Initial Catalog instead of Database? When I changed this names it started working.Reiter
Try off "Encrypt connection" and / or "trusted server certificate"Heffernan
P
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";
Penitent answered 16/4, 2018 at 14:44 Comment(4)
Is there a way to connect using the server=(LocalDb)\InstanceName format?Bookcraft
Can you provide any more information on your setup? Why are you using named instances?Penitent
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.Bookcraft
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.Penitent
S
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
Stifle answered 16/12, 2020 at 14:1 Comment(1)
I believe this is only true for Linux containers (this stung me before when I forced onto windows containers. So be careful here)Pleurodynia
G
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 =******;"
Gouache answered 19/2, 2018 at 2:25 Comment(0)
C
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.

Copolymer answered 10/12, 2018 at 9:21 Comment(1)
IP 127.0.0.1 never changes. It is a self reference just like localhost.Salop
S
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"
Scabrous answered 4/4, 2023 at 13:24 Comment(0)
T
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>
Throes answered 13/4, 2020 at 8:35 Comment(1)
This IP address doesn't work in most environments (including MacOS and Windows hosts) and you shouldn't need to look it up.Paly

© 2022 - 2025 — McMap. All rights reserved.