LLF
LLF

Reputation: 707

Docker container can't connect to SQL Server on a remote server

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;"

Upvotes: 16

Views: 34675

Answers (3)

Adam Polak Moetsi
Adam Polak Moetsi

Reputation: 543

The only solution that seemed to work for me is to put the sql server container and the .net container in the same docker network

https://github.com/microsoft/msphpsql/issues/302#issuecomment-361439294

Upvotes: 1

Saeed
Saeed

Reputation: 3775

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 !

Upvotes: 3

prisar
prisar

Reputation: 3195

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.

Upvotes: 5

Related Questions