I have a container hosted ASP.NET Core application but it can't connect to SQL Server on a remote server.
The error is:
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.
This is what I already checked:
So I think that the container can see the DB server but can't connect. What's the other thing should I check?
Thank you very much for your help.
Update
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 1433
#our sql server was use this port for connect
EXPOSE 64608
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /src
COPY Web.API.sln ./
COPY MyProject.Core/*.csproj ./MyProject.Core/
COPY MyProject.API/*.csproj ./MyProject.API/
RUN dotnet restore
COPY . .
WORKDIR /src/MyProject.API
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyProject.API.dll"]
Run Docker Command:
docker build -t myproject-api:latest .
docker run -d -p 7991:80 --name myproject-api myproject-api:latest
Connection String:
"data source=172.16.0.88\\SQL_DEV,64608; initial catalog=MyProject; persist security info=True; user id=myuser; password=mypassword;"
This is most probably because of the connection string.
Change your connection string to something like below
Server=172.16.0.88\\SQL_DEV,64608;Database=MyProject;UserId=myuser;Password=mypassword
or
Server=172.16.0.88,64608;Database=MyProject;User Id=myuser;Password=mypassword
Which ever works.
I had a same problem and I changed my Dockerfile image
I replace It
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
with this line
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic AS base
Now It works !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With