Docker container can't connect to SQL Server on a remote server
Solution 1
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.
Solution 2
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 !
LLF
Updated on June 13, 2022Comments
-
LLF almost 2 years
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:
- Try to disable firewall on host machine and DB server. : still get the error
- Edit connection string to use IP and port : still get the error
- Ping from application container to DB server: I can ping to DB server normally
- Connect to database server via SQL Server Management Studio: I can connect normally
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;"