3

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.

The error is:

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. I have deployed the same image with the same command line on another Ubuntu 14.04 server, and it connects fine.
  2. A console app running on the problem server (outside of Docker) can connect with the same connection string.

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?

Peter
  • 5,455
  • 7
  • 46
  • 68

2 Answers2

3

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

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.

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

docker network inspect bridge

Find the container and check its 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"

Or create a new docker network.

Or experiment with net=host option: docker run network settings

Ilya Chumakov
  • 23,161
  • 9
  • 86
  • 114
  • 1
    Thanks! I didn't need to override the `bip` IP address, but your information pointed me to the solution I needed, which was to create/edit `/etc/docker/daemon.json` and specify the DNS server in that environment. The Docker app now finds and connects to that db server. More information about customising the bridge network can be found in your link. – Peter Jun 06 '17 at 12:17
0

Does this help?

Connect to SQL Server database from a docker container

Also, Googling this "docker connect to sql server database" seems to return a lot of helpful results.

Ben
  • 1,820
  • 2
  • 14
  • 25