45

I'm trying to connect to my SQL Server instance running in my local computer using host.docker.internal (as recommended in https://docs.docker.com/docker-for-windows/networking/#use-cases-and-workarounds)

The host.docker.internal is successfully resolved to an IP, and it's ping-able

And I've opened up the port 1433 in my firewall configuration

Error message

Connection refused 192.168.65.2:1433

My connection string

Data Source=host.docker.internal,1433;Initial Catalog=;Persist Security Info=False;User ID=;Password=;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

docker version

Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:12:48 2018
 OS/Arch:      windows/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:22:38 2018
  OS/Arch:      linux/amd64
  Experimental: true

Docker for windows version

Docker for windows

6
  • I updated the error message Commented May 4, 2018 at 5:27
  • but not the Data Source= Commented May 4, 2018 at 5:27
  • is it ping-able inside the container? Commented May 4, 2018 at 5:31
  • Yes I’ve tried it @vitr Commented May 4, 2018 at 5:32
  • is your SQL server accepting external connections in general? have you tried to connect to it not from the docker container, but, let's say from another machine in you network? Commented May 4, 2018 at 7:46

2 Answers 2

32

If anyone have similar problem, here's how I solve it

  • Open SQL Server Configuration Manager
  • Enable TCP/IP in Server Network Configuration
  • Restart SQL Service Service

TCP/IP

If it's still not working, there are a few more things to check

  1. Firewall (open port 1433)
  2. Enable remote connections to your sql server

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

In my case I had to do few additional steps, because I wanted to access SQL Server named instance on my host machine. Trying to access "host.docker.internal\SQLSERVER,1433" data source was always a failure. Eventually I've found solution that allows to connect to the named instance. 1. Get the port of the named instance How to do it 2. Enable firewall for the port you've found 3. Change Data Source in connection string to: "host.docker.internal,<named_instance_port>"
Just wasted 2 hours on this connection issue, just to realize I forgot to restart the SQL Server service all this time ;( So glad I ran into this answer
Also check if the windows service SQL Server Browser is started
And use the connection string with host.docker.internal\\mssqlserver,1433
tcp/ip did the trick for me. dont forget to restart the sql server service
|
2

In my case I had to run this inside my SQL Server running from Docker, inside MSSQL, in order to accept remote connections:

USE master;
GO
EXEC sp_configure 'remote access', 1;
RECONFIGURE;
GO

and then use the following connection string:

Server=host.docker.internal,1433;Database=mydb;User Id=sa;Password=mypass;Connect Timeout=120;TrustServerCertificate=True;

1 Comment

host.docker.internal is a great improvement on my ever-changing host IP addresses

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.