user2695433
user2695433

Reputation: 2153

connection string for sqlserver in Docker container

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.

Upvotes: 72

Views: 107524

Answers (6)

Javier Rojano
Javier Rojano

Reputation: 982

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"

Upvotes: 3

rjminchuk
rjminchuk

Reputation: 988

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";

Upvotes: 76

alex
alex

Reputation: 402

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

Upvotes: 33

alphacoder
alphacoder

Reputation: 573

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>

Upvotes: 1

Viacheslav Yankov
Viacheslav Yankov

Reputation: 1008

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.

Upvotes: 8

David Poindexter
David Poindexter

Reputation: 516

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 =******;"

Upvotes: 25

Related Questions