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