Connect to SQL Server database from a docker container - c#

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

Related

How to connect local SQL Server database from Docker compose

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.

.NET 6 Connection to SQL Server 2008

I have 2 applications that connect to legacy SQL Server 2008 servers. These apps are currently running on .NET 3.1 and working fine.
I want to upgrade them to .NET 6, but in doing so, the app fails to connect to the database. I know this is due to this change in .NET 5:
TLS Cipher Suits
When it tries to connect, I get an error:
A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: SSL Provider, error: 31 - Encryption(ssl/tls) handshake failed)
All of the pages I have found on how to fix this require changing the openssl.cnf file, but unfortunately this is not an option for me. These applications are running on containers in Pivotal Cloud Foundry (or I guess it's called VMware Tanzu now) that I have no control over, and our platform team is unable to modify the openssl.cnf file prior to deployment. The containers run Ubuntu version 18.04. Pretty much the only things I have control over are:
The application itself, which is deployed into a folder and run as a non-admin account
Files installed with the app... I can deploy whatever files I want with the application, but they are all installed under the app folder
The command(s) run to start the application
Environment variables (I can set any I need when my app is deployed)
I also have no control over the database - it is SQL Server 2008 with no option to upgrade.
I have tried changing connection string options like TrustServerCertificate and Encrypt, but nothing works there. And I've tried that using both newer and older versions of both Microsoft.Data.SqlClient and System.Data.SqlClient. All failed.
We had a similar issue with an external API we hit, and were able to create a custom HttpMessageHandler that allowed more ciphers, but I see no option for that with a SqlConnection.
Does anyone know how I can upgrade to .NET 6 and still connect to this database without modifying the openssl.cnf file that comes with the OS?
Patch the server to support TLS 1.2. KB3135244 - TLS 1.2 support for Microsoft SQL Server

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.

How to create complete installer for ASP.Net web application

I'm programmed a web application on visual studio by ASP.Net C# and local database. then i want to create installer for this web application to do these actions on a target PC:
Install requirements for database
Install IIS on PC
Publish my Web Application as an website on IIS
NOTE
Actually, this web application should be run on a LAN that consist 3 PC and one router. one of that PC's should be a server and the web application must be installed on that system, then the other PC's getting to use from web application on a LAN.
Update
I'm used from Advanced Installer and setup works fine. but when i start the website in client system, always get this 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: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
and this is my connection string in web.config:
<add
name="SetupTestEntities"
connectionString="metadata=
res://*/Model.csdl|
res://*/Model.ssdl|
res://*/Model.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=(LocalDB)\v11.0;
attachdbfilename=|DataDirectory|\SetupTest.mdf;
integrated security=True;
connect timeout=30;
MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
You can use the Professional edition of Advanced Installer. This edition has support to install:
IIS web sites, app pools, web apps and virtual directories
install windows features without writing any scripting
install prerequisites, thus your requirements for your database
Advanced Installer also supports running SQL scripts, so you could create on the fly your databases and populate them with your defaults, but this feature is available in the Enterprise edition.
In the end Advanced Installer will build and MSI or EXE installer as output (depending on what you want), that you can use to deliver your application to any machine.
(disclaimer: I work on this product)

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