How to connect local SQL Server database from Docker compose - c#

I have created an ASP.NET Core 6 Web API project in VS2022, using a SQL Server database on my local laptop, everything was fine. After that I have added dockerfile and dockercompose then after when I run the project, I could see api from swagger but none of the end points are working since it gives error as
System.Exception: Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started.
I could understand that, from docker its not able to connect to my SQL Server database installed on my laptop, am I right?
Could you please guide me to how to resolve this error since, I'm new to Docker.

You might need to change the host that you're using to connect to your database to host.docker.internal as that's how the host machine should be referenced from inside of a Docker container. You can read more about that in the Docker docs here.

Related

Asp.net Core API Deploying with a database on IIS

I'm trying to deploy an API on IIS. But no matter how I try to do it, I can't solve the following problem.
When I try to deploy asp.net core example api there is no problem with it. When I call an endpoint there are data.
If I try to deploy my api that uses a database I got an error.
The problem that I got when I try write an endpoint is:
I think it is a database permission problem.
IIS settings are below
Connection string is below
"Local": "server=(localdb)\mssqllocaldb; database=eahworksafety;user id=eah;Password=eah;Integrated Security=false;"
MSSQL settings are below
I try to deploy an api locally on IIS webserver. I get an error when I try to reach an endpoint.
first of all, you can see more deatiales about this problem by going on your windows, search [Event viewer] ->then enter to the section windows logs->Application and see the problems.
secound, what I noticed that you are trying to connect to the localDb that exsist in visual studio by default, I have faced the same problem befor so I changed the connection string form the localDB to sql server 2019 that I instulled and worked.

Issues w/ ASP.NET Core app connecting to PostgreSQL on Docker Container

I have pulled and ran a container for PostgreSQL on my Ubuntu Server using Docker, using the instructions from these instructions.
Using Visual Studio on my Windows 10 workstation, I have developed a Blazor Web Page that connects to the PostgreSQL docker container on my Ubuntu Server using Npgsql, and uses it as the back-end. I eventually used Visual Studio's docker image building capabilities to convert my deployed ASP.NET app to a docker container that I want to run on the same Ubuntu server.
Running the container from Visual studio seems to work fine, I am successfully able to load the web page and I'm able to use the PostgreSQL as my back-end. I am also able to connect to the server without using Visual Studio, by just running the deployed image as a container on my workstation.
I want to have the Blazor web page run from the same Ubuntu Server as my PGSQL docker container, but on different containers, so that I can access the app from any computer on my local network. I pushed the container to my repository on Docker, and then pull it on my Ubuntu Server so that it could run on there.
When I run the Blazor App container on Ubuntu, I keep getting the following exception when loading a page that accesses data from my PGSQL container:
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure.
---> Npgsql.NpgsqlException (0x80004005): Exception while connecting
---> System.TimeoutException: The operation has timed out.
What I've tried:
Running the container on the same network ("pgnetwork") as the PGSQL container.
With the container running, I was successfully able to connect to my web page onto a page that doesn't use the PGSQL back-end, but it still gives me this exception when loading a page that does.
Installed ping on to the container on my Ubuntu Server, and pinged the PGSQL container. The pings were successful every time.
I am relatively new to Docker and ASP.NET core, and I'm not an IT guy, so any help or ways to troubleshoot this would be very helpful.
EDIT: One more thing I tried: installing the psql client on my docker container and attempting to connect to the PGSQL container using the same parameters as my connection string in my C# program. It wasn't able to connect.

Host ASP.NET Core MVC on local PC with IIS, host on local PC with local DB

I made a asp.net core application and I need to host it on local PC with local DB. For test I deploy and host it on my PC with IIS and it's working without problem but only with online DB, when I change connection string to my local DB I got this error: error with local database
and here is my connection string for online ( working normally) and local db ( don't work when deploy, work in visual studio)
connection strings
what I need to install/change to host my app on local PC with local DB
You should use SQL Server Express for this, not Localdb. Localdb is for VS and single desktop app use only .
Today I tried with sql express and localDB instance but I don't know how to set it. I was try some variations but unsuccessfully, I can't find tutorials for that.
Is it possible at all to host ASP.net core MVC to IIS and use local database ? My app will run on PC without connection to the internet so I need to put database locally
I solve my problem but forget to post here
I need to create new sql user with privileges to my db and set connection string like this:
"Name": "Server=localhost; Database=myDB; Trusted_Connection=false; MultipleActiveResultSets=false;User ID=UserName; Password=UserPass;"

Connect to SQL Server database from a docker container

I have docker for windows installed on my machine. There is a console application targeting .net core 1.0.0 that tries to access a SQL Server database running on a different VM. I can ping the VM running SQL Server from my machine.
When I try to run the console application using dotnet run from the command prompt on my machine it works fine. But when the same application is run inside a docker container I get a message
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: TCP
Provider, error: 40 - Could not open a connection to SQL Server)
I tried using
docker run --add-host sqldemo:<VM running sql server ip here>
but that made no difference.
Assumptions
Microsoft SQL Server 2016
Windows 10 Anniversary Update
Windows Containers
ASP.NET Core application
Add a SQL user to your SQL database.
In MS SQL expand the database
Right click on 'Security / Logins'
Select 'New Login'
Create a user name and password.
Assign a 'Server Role(s)'...I used sysadmin since I'm just testing
Under 'User Mapping' I added my new user to my database and used 'dbo' for schema.
Change SQL Authentication to allow SQL Server Authentication Mode
Right click on your database, select 'Properties / Security / Server Authentication / SQL Server and Windows Authentication Mode' radio button. Restart the MS SQL service.
Update your appsettings.json with your new user name and password
Example
"ConnectionStrings": {
"DefaultConnection": "Server=YourServerName;Database=YourDatabaseName;MultipleActiveResultSets=true;User Id=UserNameYouJustAdded;Password=PassordYouJustCreated"
},
Make sure you remove Trusted_Connection=True.
Create a Docker file
My example Docker file
FROM microsoft/dotnet:nanoserver
ARG source=.
WORKDIR /app
EXPOSE 5000
EXPOSE 1433
ENV ASPNETCORE_URLS http://+:5000
COPY $source .
Publish Application
Running from the same location as the Docker file in an elevated PowerShell
dotnet publish
docker build bin\Debug\netcoreapp1.0\publish -t aspidserver
docker run -it aspidserver cmd
I wanted to run the container and see the output as it was running in PowerShell.
Once the container was up and running in the container at the command prompt I kicked off my application.
dotnet nameOfApplication.dll
If everything went to plan one should be up and running.
You can run a docker container with network settings set to host. Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0.0.1) will refer to the docker host.
docker run --net=host ...
Then you should get the SQL Server database from inside the docker container as you do from your host.
as in this answer
SQL Server instance string connection in Linux Docker
According to Saurabh Singh from Microsoft:
The Instance name support is available in v 1.1 of .Net Core. In v1.0
of .Net Core, Instance names are not supported on OS other than
Windows.
So I don't think you can connect from .Net Core 1.0 running on Linux to an SQL Server using instance name.
Your choices seem to be:
don't use instance name
wait for .Net Core 1.1 (planned for "Fall 2016")
use pre-release version of .Net Core 1.1

Web deploy tool customization

Can we add hyperlink on web deploy tool which check on my server following prerequisite?
1..Net Framework 4.5 installed or not.
2. MVC framework installed or not.
3. Connectivity to database server from Web Server.
My application is in MVC 2?
You could create a batch script to assert these things and run it from the prompt:
msdeploy.exe -preSync="myAssertions.bat" ...
the preSync command will be executed on the remote server. Note: the batch file you will execute here needs to reside on the server because as the command specifies; preSync no syncing has happened yet.
More information about the preSync provider: http://technet.microsoft.com/en-us/library/ee619740(v=ws.10).aspx

Categories

Resources