Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to SQL Server in local machine (host) from docker using host.docker.internal

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

like image 637
Kien Chu Avatar asked May 04 '18 03:05

Kien Chu


People also ask

How does Docker container connect to local database?

A simple solution to this in a Linux machine is to use the --network=”host” option along with the Docker run command. After that, the localhost (127.0. 0.1) in your Docker container will point to the host Linux machine. This runs a Docker container with the settings of the network set to host.

How do I host a Docker internal to localhost?

Use --network="host" in your docker run command, then 127.0. 0.1 in your docker container will point to your docker host. Note: This mode only works on Docker for Linux, per the documentation.

Can I use localhost in Docker container?

The Easy OptionDocker Desktop 18.03+ for Windows and Mac supports host. docker. internal as a functioning alias for localhost . Use this string inside your containers to access your host machine.


1 Answers

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

like image 156
Kien Chu Avatar answered Oct 12 '22 00:10

Kien Chu