How do you connect to a SQL Server database on a host (or even in the cloud, as in off the local network) from a windows server-based container?
I've tried using
docker run --net="nat" ...
and
docker run --net="host" ...
to no avail.
Using -p does not seem relevant as I want to container to talk to the SQL Server database not running in a container.
I'm not sure how a custom network bridge can work here but I'm open to trying.
Related
I have a running container in my docker machine.
And I have set the port as 1423.
Then I'm using MSSQL client to connect with this container.
I used the IP address comes with host.docker.internal in the host file (C:\Windows\System32\drivers\etc). And use the port as 1423.
ex: 172.1.1.1,1423 as the server name in the MSSQL.
But I'm not able to connect to that.
expecting a way to connect to this container using MSSQL.
On the same virutal machine (remote, ubuntu), I have
An SQL Server running in a Docker
An .NET Core 2.2 (IdentityServer) application running in a docker
An instance of jwilder.nginx-proxy serving as a reverse-proxy for every web application on the machine
A multitude of other .NET Core apps
I am able to connect to all of my websites using both machine IP + port and domain name, which means the reverse proxy works as expected and the dockers are well-configured
I am able to connect to the SQL Server using SSMS from my local machine, which means that the SQL Server docker properly forwards the TCP connection on port 1433
The IdentityServer .NET Core 2 web application is able to connect to the SQL Server when run on my local machine.
The remote-docker IdentityServer application can't reach the SQL Server instance with the following error (shortened for clarity - removed stack trace)
System.Data.SqlClient.SqlException (0x80131904):
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: 40 - Could not open a connection to SQL Server) at [...]
I know that the SQL server is running and reachable from the internet, and I know that the application's code is not at fault because I tested both.
So I deduced it had to be the IdentityServer docker that was blocking the connexion. So I tried:
Using the --expose 20 command on the IdentityServer docker
Opening mapping the port 20 inside the container to some port outside -p 45264:20 in addition to the already exposed port 80
I originally worked on using the port 1433 on both sides of the mapping but since it didn't work I tried using an other port on the outside (20). Didn't change anything
Here is the connexion string used by the IdentityServer (sensitive data hidden):
Data Source=***.***.***.***,20;Initial Catalog=Identity;Persist Security Info=True;User ID=**;Password=******************
Why can't my IdentityServer docker reach the SQL Server docker while the SQL Server itself is perfectly reachable? How can I make this setup work?
When wrapping SQL server into Docker, the first thing to anticipate is the way you connect. SQL Server prefers named pipes and you have to explicitly set mode to tcp.
If connection is done locally, don't use localhost, change it to 127.0.0.1. Also writing explicit tcp: prefix may help, like this: Server=tcp:x.y.z.q,1433
As I understood you run Sql Server and IdentityServer (which has connection problem) in separate docker containers.
If this is so then referring to localhost (i.e. 127.0.0.1) is not correct. Because in this case IdentityServer tries to connect to itself. This would work if the IdentityServer have run on the host machine, since you forward SQL server port to it. But in your case, you should connect to the SQL server container IP instead.
Considering all above I see three options for you to solve this:
You can get ip address of SQL Server container by running docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <sql_container_name_or_id>
Run SQL container with static IP via docker run --ip <static_ip_value> <sql_container_name_or_id> and then use static ip you have specified in connection string.
Run SQL container with specified host name via docker run --hostname <sql_host_name> <sql_container_name_or_id> and then use specified hostname in the connection string.
It is up to you which way to go.
Use tcp, 127.0.0.1 and host port to connect. Mention in the identity server docker settings that it depends on sql database server container. Like this,
identityservice:
...
depends_on:
- sqldataservice
This way the database container will be made available first.
"ConnectionString": "Server=tcp:127.0.0.1,8433;Database=dbname;User Id=sa;Password=abc#1234;"
I ended up giving up on getting this to work using a single host, so I simply decided to have the SQL Server run in a separate machine.
I have faced with very strange issue.
Host OS is Windows 10 Pro, installed Docker for Windows.
When I run microsoft/mssql-server-windows-developer container using simple command
docker run -d -p 1433:1433 -e sa_password=<SA_PASSWORD> -e ACCEPT_EULA=Y microsoft/mssql-server-windows-developer
I can connect to SQL Server via MS SQL Management Studio using server name=localhost. But I can't open Server Properties or open "Attach DB" wizard. There is the same error all the time "The semaphore timeout period has expired".
I can run container with attached DBs and I can execute sql inside MS SQL Management Studio. But when I run my Web Application - I get the same error "The semaphore timeout period has expired" on connect to DB.
Then I tried to connect to SQL Server using IP address which I get via docker container inspect - in my case it was 172.28.93.83. And everything worked as it should - I can manage with SQL Server, my Web Application connects to the DB, etc.
So, what is the issue there? I still want to use localhost to connect to server to have unified connection string in all developers environments.
When I tried to run Linux container - everything worked fine using localhost to connect to server.
I could be important - before I decided to use docker to work with MS SQL Server I had MS SQL Server installed and then uninstalled it. Maybe it made some changes in a routing or network which I have to remove...
I would appreciate any help
Try with increase timeout seconds in connection string.
I solved a similar incident changing the metric to the card generated by Docker.
I put 5000.
Luck.
Context: I am trying to build a development SQL Server that I can continue to learn SQL on and use Microsoft SQL Server Management Studio (SSMS) to access on a Windows PC.
So I have the AdventureWorks database sitting on a Docker Container for MS SQL Server 2017 running on a DigitalOcean Ubuntu 16.04 box. From my Mac I can remote SSH in to the server, access the container and query the database.
However I wish to use SSMS on my Windows PC and am unsure how I begin to connect to the remote box. In the picture below, there are no options to specify an SSH key or to even login in to the Ubuntu box, only to access the SQL server.
Is this even possible?
connect via SSMS using the public IP address, followed by comma separator and then the port (xxx.xx.xx.xxx,port)
You'll also need the sa credentials to make this work.
In your case, Server Name input will become Server IP, port#
No need of ssh, you just need to expose SQL Server service to the internet.
When Using SQL Server Management Studio to connect to the docker container on local machine, you can use localhost ip. In that case Server Name input will become 127.0.0.1,port#
Following instruction of this site solved it for me on Windows Containers:
https://www.sqlshack.com/sql-server-with-a-docker-container-on-windows-server-2016/
It was possible to connect to SQL Server instantly from SSMS.
Give it a try if this is also working on Linux containers with this command:
docker run --name mssqltrek-con1 -d -p 1433:1433 -e sa_password=My$eCurePwd123# -e ACCEPT_EULA=Y microsoft/mssql-server-windows-developer
After running this you can retrieve the correct ip with:
docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" mssqltrek-con1
In my case it was the Cisco VPN that was blocking the host to connect to container IP. I had to uncheck the "Cisco Any Connect Network Access Manager" as shown in the image below for it to work. It ensures that the VPN no longer manages the connection.
You can connect to SQL docker server
Using IP of Machine on which docker image is hosted,port
IP_Of_Machine,Port
Provide User - sa (default) and password.
you can also do it command>
docker exec -it <container_id|container_name> /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P <your_password>
This isn't exactly the same problem as the OP's but I created a sql docker container and then couldn't log in to it from SQL Server Management Studio. It turned out that the sa password I used wasn't secure enough. Once I deleted my container and recreated one with a more secure password it let me connect to it from SQL Server Management Studio. If you do not specify a strong enough password it still creates the container OK - you just can't log in to it!
Hopefully this will save someone some time.
I benefited from the answers on this page, however, i had to go through my own tweak. for some reason, in my case, it didnt accept localhost,1433 but it did accept 127.0.0.1,1433
In my case I was using docker-compose and was unable to connect. I fixed this by explicitly specifying the port in the docker compose file.
mysqldb:
ports:
- "1433:1433"
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=SuperSecretPassword!23
image: registry/mysqlimage
I also faced same issue when I try to connect through SSMS, even I tried with localhost,1433 it doesn't help me out.
enter image description here
After some R&D I found the way to solve it
Open Kubernetes which will be installed as part of docker toolbox
(mine was win 10 home which will not support hyper-v)
click on sql container which will be in left side of app
copy the IP address which will be in right side of app
use IP address with port like XXX.XXX.XX.XX,1433 along with credentials in SSMS
enter image description here
I'm trying to connect to my SQL Server Developer edition on my local Windows 10 Pro machine from a docker image created using Visual Studio Tools for Docker Desktop for Windows. I've followed the tutorial here. Which helped me make sure that SQL Server is functional and exposed to the outside world.
My preference would be to somehow start docker with NET=HOST and just use . to access my DB. But, I'm not sure how to do that and I'm not sure if that even works on Windows.
Now I can't seem to get the connection right to actually connect to SQL. I've tried these:
Server={MyIPv4Address}:434;Database=MyDB;
MultipleActiveResultSets=true;User
Id=DeveloperLocalHost;Password=MyAwesomePassword
Server={MyIPv4Address};Database=MyDB;
MultipleActiveResultSets=true;User
Id=DeveloperLocalHost;Password=MyAwesomePassword
Server=.;Database=MyDB;
MultipleActiveResultSets=true;User
Id=DeveloperLocalHost;Password=MyAwesomePassword
I've tried many others from tutorials I have looked at online but don't remember them all. I'm a bit perplexed.
I would expect the second method (actual host IP and default 1433 port) to work as long as your SQL instance is configured to allow remote connections. Run this PS command from your container to verify port connectivity:
echo ((new-object Net.Sockets.TcpClient).Client.Connect("MyIPv4Address", "1433")) "connection successful"
Once you verify connectivity, you should be able to start the container with an environment variable and use that for your connection.