3

I am trying to create a login web application in visual studio (.NET core web app). I am using Azure Data Studio(ADS) to connect to the local database for the application. But when I run the application using docker, the web app loaded successfully but it fails to load the local database.

Now, after looking for some solutions online, I pulled an image of the SQL Server 2019 container on Docker and connected it to ADS. I am unable to set up a connection string in visual studio to the SQL Server database hosted inside the docker on ADS.

Can someone provide any leads on how can I set up the connection between my login app and SQL server container inside Docker on ADS?

Error Message in Nugget Package Manager Console

enter image description here

sk5991
  • 45
  • 6
  • Please refer to the answer below. – Viswanatha Swamy Jun 26 '21 at 04:48
  • Please update your query. It has 3 concepts. A. We need to connect to SQL Server Docker container using Azure Data Studio, B. We need to migrate the data using EF Core Tools, and C. We need to connect to SQL Server Docker Container through Web App running as another Docker Container. – Viswanatha Swamy Jun 26 '21 at 11:02

1 Answers1

0

This is one of the ways Connecting to SQL Server container hosted inside Docker.

Assuming that you have executed the below-mentioned command to execute the SQL Server 2019 container.

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Sample123$" -p 1433:1433 --name mssql2019indocker -d mcr.microsoft.com/mssql/server:latest

Azure Data Studio:

You can use server=localhost, Authentication Type=SQL Login, User name = sa, and password = Sample123$. These details are based on the command you have executed which creating the container. Please refer to the image below.

Azure Data Studio to Login into SQL Server 2019 Docker Container SQL Server 2019 Docker Container

Note: If you are using the EF Core tools to migrate the database the server should be "localhost" as shown in the Azure Data Studio login image.

.NET Core application connecting to Docker Container

Here the situation is .NET core application is executing in one Docker container, and SQL Server 2019 is executing in another container.

Please execute the below-mentioned command to find out the IPAddress of the SQL Server 2019 Docker container. Please refer to the image below. In my scenario, the IPAddress is 172.17.0.3.

docker inspect <ContainerIdOfSQLServer2019>

Once you get IPAddress, please update it in the below-mentioned connection string.

Server=<IPAddressOfTheSqlServer2019DockerContainer>;Database=myDataBase;User Id=myUsername;Password=myPassword;

Finding IP Address of SQL Server 2019 Docker container Finding IP Address of SQL Server 2019 Docker container

Executing the Web App with Docker Configuration Executing the Web App with Docker Configuration

Viswanatha Swamy
  • 699
  • 1
  • 10
  • 17
  • Thanks, Vishwanatha...The above information is really helpful. While updating database I am receiving the following error "A network-related or instance-specific error occurred while establishing a connection to SQL Server. "The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)". – sk5991 Jun 26 '21 at 08:42
  • I also checked the SQL SERVER CONFIGURATION MANAGER. SQL Server Services (SQL SERVER, SQL SERVER AGENT, SQL SERVER BROWSER), as well as SQL Server Network Configuration, have every component (Named Pipes,TCP/IP, Shared Memory) as active – sk5991 Jun 26 '21 at 08:46
  • "ConnectionStrings": {"BookStoreDbContextConnection": "Server=172.17.0.2;Database=LoginDB;User Id=user01;Password=User@43567!" } – sk5991 Jun 26 '21 at 09:41
  • IP Address in CMD after Docker inspect : "IPAddress": "172.17.0.2", – sk5991 Jun 26 '21 at 09:42
  • The issue arises, when after setting up the connection string in appsettings.json, in nugget package manager console, I use 1) Add-Migration "Initial-Create" and 2) Update-Database . While Updating Database it shows me the error – sk5991 Jun 26 '21 at 10:52
  • I have included docker support in the application...but haven't run the application in docker – sk5991 Jun 26 '21 at 11:05