简体   繁体   English

Docker 容器无法连接到远程服务器上的 SQL 服务器

[英]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.我有一个容器托管 ASP.NET 核心应用程序,但它无法连接到远程服务器上的 SQL 服务器。

The error is:错误是:

A network-related or instance-specific error occurred while establishing a connection to SQL Server.与 SQL 服务器建立连接时发生与网络相关或特定于实例的错误。 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.验证实例名称是否正确,并且 SQL 服务器配置为允许远程连接。

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编辑连接字符串以使用 IP 和端口:仍然得到错误
  • Ping from application container to DB server: I can ping to DB server normally从应用程序容器 Ping 到数据库服务器:我可以正常 ping 到数据库服务器
  • Connect to database server via SQL Server Management Studio: I can connect normally通过SQL Server Management Studio连接数据库服务器:可以正常连接

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: 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 命令:

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我遇到了同样的问题,我更改了我的 Dockerfile 图像

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 !现在它起作用了!

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似乎对我有用的唯一解决方案是将 sql 服务器容器和 .net 容器放在同一个 docker 网络中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM