什么会阻止在Docker容器中运行的代码连接到单独服务器上的数据库? [英] What would prevent code running in a Docker container from connecting to a database on a separate server?

查看:111
本文介绍了什么会阻止在Docker容器中运行的代码连接到单独服务器上的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET Core 1.1应用程序在Ubuntu 14.04的Docker容器中运行,但无法连接到在单独的服务器上运行的SQL Server数据库。

I have a .NET Core 1.1 app running in a Docker container on Ubuntu 14.04, and it fails to connect to the SQL Server database running on a separate server.

错误为:


未处理的异常:System.Data.SqlClient.SqlException:建立时发生与网络相关或特定于实例的错误与SQL Server的连接。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (提供者:TCP Provider,错误:25-连接字符串无效)

Unhandled Exception: System.Data.SqlClient.SqlException: 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. (provider: TCP Provider, error: 25 - Connection string is not valid)




  1. 我已经部署了在另一台Ubuntu 14.04服务器上具有相同命令行的同一映像,并且连接正常。

  2. 在问题服务器(Docker外部)上运行的控制台应用程序可以使用相同的连接字符串进行连接。

据我所知,默认情况下,在容器中运行的应用可以访问外部网络可能会阻止此连接?

As far as I can see from the documentation, an app running in a container has access to the external network by default, so what could be blocking this connection?

推荐答案


在容器中运行的应用可以访问外部网络默认情况下,

an app running in a container has access to the external network by default

只有将有效IP地址分配给容器后,它才可以访问。有时Docker为容器选择的IP可能与外部网络冲突。

It could have access only if a valid IP address is assigned to the container. Sometimes the IP which Docker choose for the container can conflict with external networks.

默认情况下,容器在 bridge 中运行网络,所以请看一下:

By default, containers run in bridge network, so look at it:

docker network inspect bridge

找到容器并检查其IP。

Find the container and check its IP.

要解决冲突,可以自定义 网桥网络并设置 bip 参数以更改网络的IP范围(配置文件的位置取决于主机的操作系统):

To resolve conflicts, it is possible to customize bridge network and set bip parameter to change the network's IP range (config file location depends on host's OS):

"bip": "192.168.1.5/24"

创建新的docker网络。

Or create a new docker network.

或尝试使用 net = host 选项: docker运行网络设置

这篇关于什么会阻止在Docker容器中运行的代码连接到单独服务器上的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆