c# - Connect to SQL Server database from a docker container -
i have docker windows installed on machine. there console application targeting .net core 1.0.0 tries access sql server database running on different vm. can ping vm running sql server machine.
when try run console application using dotnet run
command prompt on machine works fine. when same application run inside docker container message
a network-related or instance-specific error occurred while establishing connection sql server. server not found or not accessible. verify instance name correct , sql server configured allow remote connections. (provider: tcp provider, error: 40 - not open connection sql server)
i tried using
docker run --add-host sqldemo:<vm running sql server ip here>
but made no difference.
assumptions
- microsoft sql server 2016
- windows 10 anniversary update
- windows containers
- asp.net core application
add sql user sql database.
- in ms sql expand database
- right click on 'security / logins'
- select 'new login'
- create user name , password.
- assign 'server role(s)'...i used sysadmin since i'm testing
- under 'user mapping' added new user database , used 'dbo' schema.
change sql authentication allow sql server authentication mode
right click on database, select 'properties / security / server authentication / sql server , windows authentication mode' radio button. restart ms sql service.
update appsettings.json new user name , password
example "connectionstrings": { "defaultconnection": "server=yourservername;database=yourdatabasename;multipleactiveresultsets=true;user id=usernameyoujustadded;password=passordyoujustcreated" },
make sure remove trusted_connection=true
.
create docker file
my example docker file
microsoft/dotnet:nanoserver arg source=. workdir /app expose 5000 expose 1433 env aspnetcore_urls http://+:5000 copy $source .
publish application
running same location docker file in elevated powershell
dotnet publish
docker build bin\debug\netcoreapp1.0\publish -t aspidserver
docker run -it aspidserver cmd
i wanted run container , see output running in powershell.
once container , running in container @ command prompt kicked off application.
dotnet nameofapplication.dll
if went plan 1 should , running.
Comments
Post a Comment