73

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? 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. 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 Commented Aug 16, 2017 at 11:53

6 Answers 6

76
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";
Sign up to request clarification or add additional context in comments.

5 Comments

Is there a way to connect using the server=(LocalDb)\InstanceName format?
Can you provide any more information on your setup? Why are you using named instances?
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.
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.
6 years later I just want to say.. I was hung up on trying to answer the question above when the actual answer is "don't." orchestrate your own local database copy and point at that instead. I like Azure SQL Edge, and it'll let you auto bootstrap it with a dacpac if you care to.
35

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 Comment

I believe this is only true for Linux containers (this stung me before when I forced onto windows containers. So be careful here)
25

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

Comments

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 Comment

IP 127.0.0.1 never changes. It is a self reference just like localhost.
3

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"

Comments

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 Comment

This IP address doesn't work in most environments (including MacOS and Windows hosts) and you shouldn't need to look it up.

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.