3

I need help in writing a connection string to connect to a SQL Server database which is running in a docker container.

It's a .NET app that needs to be connected, but initially I want to test the connections with the database I have on SQL Server on docker.

IDE being used is Riders, OS is Mac OS.

2
  • do you have any idea about what connection string will contain ?? Commented Oct 14, 2017 at 4:27
  • Yes, little bit as I am beginner. It needs server name, user name, pass word, port number and we can also mention a specific db name. Commented Oct 14, 2017 at 15:44

1 Answer 1

6

If your .NET app is running into docker in the same solution, you have to use the name of sql server container.

docker-compose.yml

sqlserver:
    image: 'microsoft/mssql-server-linux:2017-latest'
    container_name: sqlserver
    volumes:
      - 'mssql-server-linux-data:/var/opt/mssql/data'
    environment:
      - ACCEPT_EULA='Y'
      - SA_PASSWORD=xxxxxxxx
    ports:
      - '1433:1433'

Your connection string will be :

"ConnectionString": "Server=sqlserver;Database=xxxx;User Id=xx;Password=xxx;"

Otherwise, use in Server parameter the IP address for the computer hosting the container.

Sign up to request clarification or add additional context in comments.

2 Comments

Hello @Raul Martin Ramos. Thank you for your help. However am running my app on my Mac and SQL server is on docker. In the visual studio(changed me IDE from previous) should I add the docker file above and connect it. Am a bit confused about all this process.
Hi Bhavesh, If docker is running in you mac machine, you should access by localhost. Example : var connection = new SqlConnection("Server=localhost;Database=xxxxx;User Id=xx;Password=xxxx;");

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.